Functional Test Failing

I am a Ruby/Rails newby, currently reading "Simply Rails 2" by Patrick
Lenz. I am up to the section on functional testing (of the

The first functional test tests the index action of the controller.
Unlike the book however, my functional test fails, a partial listing
of the output is shown below:

Loaded suite /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/
Finished in 0.163936 seconds.

  1) Error:
ActionView::TemplateError: You have a nil object when you didn't
expect it!
The error occurred while evaluating
    On line #4 of stories/index.html.erb

    1: <p>The time is: <%= @current_time %></p>
    3: A random link:
    4: <%= link_to(, %>

It seems @story is pointing to a nil object rather than a Story
object. The index action works fine when I manually try it in my
browser so I am thinking it could be that Rails is not finding any
data in the database when it runs this test. I assume the database in
this case is the test database. Querying the Stories table in the
test database AFTER running this test returns no rows.

Below is the test method which is failing:

class StoriesControllerTest < ActionController::TestCase
  def test_should_show_new
    get :new
    assert_response :success
    assert_template 'new'
    assert_not_nil assigns(:story)

Any ideas?

   R u testing test_should_show_new (assume to test new action) or
test_should_show_index (test index action?) ?

Sorry, I listed the test_should_show_new method, which works fine.
The method relating to the testing error is:

  def test_should_show_index
    get :index
    assert_response :success
    assert_template 'index'
    assert_not_nil assigns(:story)

And also you have to
populate testdb using fixtures or can use factories Have you edited
fixtures/stories.yml and enter data to it? After setting this just as a
test, try the test test_should_show_index like

I don't know what factories are. File test/fixtures/stories.yml has
the following contents:

  name: My shiny weblog

  name: SitePoint Forums

If I query the test database AFTER the test shouldn't I find the two
rows in the Stories table?

$ sqlite3 db/test.sqlite3
SQLite version 3.4.0
Enter ".help" for instructions

select count(*) from Stories;


ruby test/functionals/stories_controller_test -n test_should_show_index

Thanks for the above command; I have been using:

rake test:functionals

I started coding the app under Rails 1.2.6 and then upgraded to Rails
2.0.2 to be in line with the version used by the Simply Rails 2 book.
Do you think this could be the cause of the problem?

Since the other functional test works I guess I can discount the
upgrade causing the issue.

The test succeeds if I change the index method in my StoriesController
class so it creates a Story object rather than retrieving one from the
test database.

Old index method...

  def index
    @current_time =

    @story = Story.find(:first, :order => 'RANDOM()')

New index method...

  def index
    @current_time =

    @story = = 'A test' = ''

It seems the problem is that there is no rows in the Stories table in
the test database.

Can someone please explain how the test database is populated?

Thanks for the link Sijo, I'll check it out.

For whatever reason, data isn't being populated into the test database
when I run my tests. I suspect the Rails upgrade has something to do
with it.

Anyway, I quickly re-created the app and all is fine now and the test
database now has the same data as the development database (without
any intervention from me).

Thanks for your help.