SiteControllerTest error. <nil> expected to not be nil


#1

I have been following a tutorial from a (semi-outdated) textbook
describing how to create a rails social networking app. I have reached
the testing phase and have encountered an error I can’t seem to solve.
It originates from an asset call in the site_controller_test.rb file.

Error message:

  1. Failure:
    test_registration_success [test/functional/
    site_controller_test.rb:49]:
    expected to not be nil.

4 tests, 9 assertions, 1 failures, 0 errors

site_controller_test.rb:

require File.dirname(FILE) + ‘/…/test_helper’
require ‘site_controller’

Re-raise errors caught by the controller.

class SiteController; def rescue_action(e) raise e end; end

class SiteControllerTest < ActionController::TestCase
def setup
@controller = SiteController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end

Replace this with your real tests.

def test_truth
assert true
end

def test_index
get :index
title = assigns(:title)
assert_equal “The Water Cooler Site”, title
assert_response :success
assert_template “index”
end

Make sure the registration page responds with the proper form.

def test_registration_page
get :register
title = assigns(:title)
assert_equal “Registration Page”, title
assert_response :success
assert_template “register”
end

Test a valid registration.

def test_registration_success
post :register, :user=>{ :user_name => “rayjay6295”,
:email => “removed_email_address@domain.invalid”,
:password => “password”}
# Test assignment of user.
user = assigns(:user)
assert_not_nil user
# Test new user in database.

new_user = User.find_by_user_name_and_password(user.user_name,
                        user.password)
assert_not_nil new_user
assert_equal new_user, user
# Test flash and redirect.
assert_equal "User #{new_user.user_name} created!", flash[:notice]
assert_redirected_to :action => "index"

end
end

////////////////////////////////////////////////////////////////////

site_controller.rb:

class SiteController < ApplicationController
def index
@title= “The Water Cooler Site”
end

def personal_page
@title = “Welcome, YOUR NAME HERE”
end

def register
@title = “Registration Page”
if request.post?
@user = User.new(params[:user])
if @user.save
flash[:notice] = “User #{@user.user_name} created!”
redirect_to :action => “index”
end
end
if request.post?
# Output goes to log file (logs/development.log in development
mode)
logger.info params[:user].inspect
end
#if request.post?
#Output goes to browser
#raise params[:user].inspect
#end
end

protect_from_forgery :only => [:create, :update, :destroy]
end
/////////////////////////////////////////////

from databse.yml:

development:
adapter: mysql
encoding: utf8
reconnect: false
database: watercooler_development
pool: 5
username: root
password: MyNewPass
host: localhost

test:
adapter: mysql
encoding: utf8
reconnect: false
database: watercooler_test
pool: 5
username: root
password: MyNewPass
host: localhost

///////////////////////////////////////////

create_users.rb:

class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :user_name, :string
t.column :first_name, :string
t.column :last_name, :string
t.column :fav_show, :string
t.column :email, :string
t.column :password, :string
t.timestamps
end
end

def self.down
drop_table :users
end
end
///////////////////////////////////////////////////

register.html.erb:

Register

<% form_for :user do |form| %> Enter Your Details <%= error_messages_for "user" %>
User Name: <%= form.text_field :user_name, :size => User::USER_NAME_SIZE, :maxlength => 40 %>
Email: <%= form.text_field :email, :size => User::EMAIL_SIZE, :maxlength => 50 %>
Password: <%= form.password_field :password, :size => User::PASSWORD_SIZE, :maxlength => 40 %>
Re-type Password: <%= form.password_field :password, :size => User::PASSWORD_SIZE, :maxlength => 40 %>
<div class="form_row">
  <label for="first_name">First Name:</label>
  <%= form.text_field :first_name, :size =>

User::FIRST_NAME_SIZE, :maxlength => 40 %>

<div class="form_row">
  <label for="last_name">Last Name:</label>
  <%= form.text_field :last_name, :size =>

User::LAST_NAME_SIZE, :maxlength => 40 %>

<div class="form_row">
  <label for="user_name">Favorite TV Show:</label>
  <%= form.text_field :fav_show, :size =>

User::FAV_SHOW_SIZE, :maxlength => 40 %>

<div class="button">
  <%= submit_tag "Register!", :class => "submit" %>
</div>
<% end %>

////////////////////////////////////

Thanks for any help. It seems like it can retrieve a user from the
post method fine, but once it tries to either insert or pull from the
database it runs into trouble.


#2

Did you know that it is possible to use the debugger when testing? This
can
be very useful sometimes as you can break into your code during the test
and
see what is happening.

Colin

2009/5/8 removed_email_address@domain.invalid removed_email_address@domain.invalid