# SiteControllerTest error. \<nil\> expected to not be nil

**URL:** <https://www.ruby-forum.com/t/sitecontrollertest-error-nil-expected-to-not-be-nil/167573>\
**Category:** Rails\
**Created:** [May 9, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/sitecontrollertest-error-nil-expected-to-not-be-nil/167573 "2009-05-09T17:16:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 9, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/sitecontrollertest-error-nil-expected-to-not-be-nil/167573/1 "2009-05-09T17:16:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 9, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/sitecontrollertest-error-nil-expected-to-not-be-nil/167573/2 "2009-05-09T18:00:20Z")

</div>

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](mailto:removed_email_address@domain.invalid)
