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:
Time <%= @current_time %>
A story <%= @story.name %>
A story (more polite): <%= link_to @story.name, @story.link %></
p>
Random history: <%= link_to @rand_story.name, @rand_story.link
%>
- 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.
On Wed, 2008-07-23 at 10:03 -0700, txapelgorri wrote:
@rand_story = Story.find(:first, :order => 'RANDOM()')
class StoriesControllerTest < ActionController::TestCase
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.
does test database have data? Are you using fixtures to populate test
database?
if you run…
script/console test
@story = Story.find_by_name(‘txapelgorri’)
does it also generate an error?
Craig
Quoting txapelgorri [email protected]:
@story = Story.find_by_name('txapelgorri')
- And finally this is the test:
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.
2008/7/23 txapelgorri [email protected]:
@story = Story.find_by_name(‘txapelgorri’)
- And finally this is the test:
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.
–
–
Visitame en http://sinanimodelucro.net
Usa GNU/Linux, tus úlceras te lo agradecerán.
Realizado con Software Libre.