Simple functional test fails

Hi there:

I'm starting with rails and I founded and issue that I can't figure out how to solve:

- This is the controller: class StoriesController < ApplicationController   def index     @current_time = Time.now     @story = Story.find_by_name('txapelgorri')     @rand_story = Story.find(:first, :order => 'RANDOM()')   end end - This is the view:     <p>Time <%= @current_time %></p>     <p>A story <a href="<%= @story.link %>"><%= @story.name %></a></p>     <p>A story (more polite): <%= link_to @story.name, @story.link %></

    <p>Random history: <%= link_to @rand_story.name, @rand_story.link %></p> - And finally this is the test: class StoriesControllerTest < ActionController::TestCase   def test_should_show_index     get :index     assert_response :success     assert_template 'index'     assert_not_nil assigns(:story)   end end

When running "rake test:functionals" it shows an error: "The error occurred while evaluating nil.link on line #2 of stories/ index.html.erb". This error belongs to "assert_not_nil assigns(:story)" at the test file (so it says the console). Isn't supposed to be already a "not nil" instance variable when I declared it at controller (Story.find_by_name, blah, blah)?.

Thanks in advance, txapelgorri.

Hi there:

I'm starting with rails and I founded and issue that I can't figure out how to solve:

- This is the controller: class StoriesController < ApplicationController   def index     @current_time = Time.now     @story = Story.find_by_name('txapelgorri')     @rand_story = Story.find(:first, :order => 'RANDOM()')   end end - This is the view:     <p>Time <%= @current_time %></p>     <p>A story <a href="<%= @story.link %>"><%= @story.name %></a></p>     <p>A story (more polite): <%= link_to @story.name, @story.link %></ >     <p>Random history: <%= link_to @rand_story.name, @rand_story.link %></p> - And finally this is the test: class StoriesControllerTest < ActionController::TestCase   def test_should_show_index     get :index     assert_response :success     assert_template 'index'     assert_not_nil assigns(:story)   end end

When running "rake test:functionals" it shows an error: "The error occurred while evaluating nil.link on line #2 of stories/ index.html.erb". This error belongs to "assert_not_nil assigns(:story)" at the test file (so it says the console). Isn't supposed to be already a "not nil" instance variable when I declared it at controller (Story.find_by_name, blah, blah)?.

Thanks in advance, txapelgorri.

Quoting txapelgorri <ibon.castilla@gmail.com>:

Hi there:

I'm starting with rails and I founded and issue that I can't figure out how to solve:

- This is the controller: class StoriesController < ApplicationController   def index     @current_time = Time.now     @story = Story.find_by_name('txapelgorri')     @rand_story = Story.find(:first, :order => 'RANDOM()')   end end - This is the view:     <p>Time <%= @current_time %></p>     <p>A story <a href="<%= @story.link %>"><%= @story.name %></a></p>     <p>A story (more polite): <%= link_to @story.name, @story.link %></ >     <p>Random history: <%= link_to @rand_story.name, @rand_story.link %></p> - And finally this is the test: class StoriesControllerTest < ActionController::TestCase   def test_should_show_index     get :index     assert_response :success     assert_template 'index'     assert_not_nil assigns(:story)   end end

When running "rake test:functionals" it shows an error: "The error occurred while evaluating nil.link on line #2 of stories/ index.html.erb". This error belongs to "assert_not_nil assigns(:story)" at the test file (so it says the console). Isn't supposed to be already a "not nil" instance variable when I declared it at controller (Story.find_by_name, blah, blah)?.

You are not loading any fixtures, so the test database may very well be empty. It the database is empty find(:first) will return nil. You may be confusing nil with undefined.

HTH,   Jeffrey

Thanks both for answering. I was playing around with the code and I probably miss to change the instance variable at any of the three files :S It finally get solved by it self. Thanks again. Cheers, txapelgorri.