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
controller).

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/
rake_test_loader
Started
E.
Finished in 0.163936 seconds.

  1. Error:
    test_should_show_index(StoriesControllerTest):
    ActionView::TemplateError: You have a nil object when you didn’t
    expect it!
    The error occurred while evaluating nil.name
    On line #4 of stories/index.html.erb
1: <p>The time is: <%= @current_time %></p>
2:
3: A random link:
4: <%= link_to(@story.name, @story.link) %>

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

Any ideas?

Hi
R u testing test_should_show_new (assume to test new action) or
test_should_show_index (test index action?) ? 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

ruby test/functionals/stories_controller_test -n test_should_show_index

Sijo

On Jul 29, 6:45 pm, Sijo Kg [email protected] wrote:

Hi
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)
end

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:

one:
name: My shiny weblog
link: http://poocs.net/

two:
name: SitePoint Forums
link: SitePoint Forums | Web Development & Design Community

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
sqlite> select count(*) from Stories;
0
sqlite>

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
Hi

Is all your other tests work? If not please check each of following

in 1.2.6 the test inherits from < Test::Unit::TestCase and in case of
2.x it from ActionController::TestCase
In test/test_helper.rb search

fixtures :all #This loads all fixtures

Or you can add to your stories_controller_test at above like
fixtures :stories #loads only stories fixtures from stories.yml

  run  rake db:test:prepare

Now run your failed test

Sijo

On Jul 30, 12:18 am, shusseina [email protected] wrote:

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 = Time.now

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

end

New index method…

def index
@current_time = Time.now

@story = Story.new
@story.name = 'A test'
@story.link = 'http://a_test.com/'

end

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.

Regards

Hi
To get a good understanding read this. I think you are totally
confused

Sijo