# Simple functional test fails

**URL:** <https://www.ruby-forum.com/t/simple-functional-test-fails/143953>\
**Category:** Rails\
**Created:** [July 24, 2008, 4:16am UTC](https://www.ruby-forum.com/t/simple-functional-test-fails/143953 "2008-07-24T04:16:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![txapelgorri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f8ce5/32.png) [@txapelgorri](https://www.ruby-forum.com/u/txapelgorri)\
**Post date:** [July 24, 2008, 4:16am UTC](https://www.ruby-forum.com/t/simple-functional-test-fails/143953/1 "2008-07-24T04:16:04Z")

</div>

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
- Time \<%= @current\_time %\>

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.

---

<div class="post-metadata">

**Author:** ![txapelgorri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f8ce5/32.png) [@txapelgorri](https://www.ruby-forum.com/u/txapelgorri)\
**Post date:** [July 24, 2008, 4:26am UTC](https://www.ruby-forum.com/t/simple-functional-test-fails/143953/2 "2008-07-24T04:26:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![txapelgorri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f8ce5/32.png) [@txapelgorri](https://www.ruby-forum.com/u/txapelgorri)\
**Post date:** [July 24, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/simple-functional-test-fails/143953/3 "2008-07-24T18:43:16Z")

</div>

Quoting txapelgorri [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> @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

---

<div class="post-metadata">

**Author:** ![txapelgorri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f8ce5/32.png) [@txapelgorri](https://www.ruby-forum.com/u/txapelgorri)\
**Post date:** [July 25, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/simple-functional-test-fails/143953/4 "2008-07-25T23:17:12Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> @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](http://sinanimodelucro.net)  
Usa GNU/Linux, tus úlceras te lo agradecerán.

## Realizado con Software Libre.

## GPG public key at [http://sinanimodelucro.net/txapelgorri.asc](http://sinanimodelucro.net/txapelgorri.asc) Finderprint: 9402 CEBF 0274 F213 8B88 1209 7B2D 70B3 843E F99C
