Forum: Ruby on Rails SiteControllerTest error. <nil> expected to not be nil

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2009-05-09 19:16
(Received via mailing list)
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<SiteControllerTest> [test/functional/
site_controller_test.rb:49]:
<nil> 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:

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

    <div class="form_row">
      <label for="first_name">First Name:</label>
      <%= form.text_field :first_name, :size =>
User::FIRST_NAME_SIZE, :maxlength => 40 %>
    </div>

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

    <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>

    <div class="button">
      <%= submit_tag "Register!", :class => "submit" %>
    </div>
  </fieldset>
<% 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.
Colin L. (Guest)
on 2009-05-09 20:00
(Received via mailing list)
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>
This topic is locked and can not be replied to.