# Functional Test Failing

**URL:** <https://www.ruby-forum.com/t/functional-test-failing/172875>\
**Category:** Rails\
**Created:** [July 29, 2009, 10:34am UTC](https://www.ruby-forum.com/t/functional-test-failing/172875 "2009-07-29T10:34:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 29, 2009, 10:34am UTC](https://www.ruby-forum.com/t/functional-test-failing/172875/1 "2009-07-29T10:34:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 29, 2009, 10:45am UTC](https://www.ruby-forum.com/t/functional-test-failing/172875/2 "2009-07-29T10:45:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 29, 2009, 4:19pm UTC](https://www.ruby-forum.com/t/functional-test-failing/172875/3 "2009-07-29T16:19:27Z")

</div>

On Jul 29, 6:45 pm, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://poocs.net/)

two:  
name: SitePoint Forums  
link: [SitePoint Forums | Web Development & Design Community](http://www.sitepoint.com/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  
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?

---

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 30, 2009, 6:20am UTC](https://www.ruby-forum.com/t/functional-test-failing/172875/4 "2009-07-30T06:20:21Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 29, 2009, 4:23pm UTC](https://www.ruby-forum.com/t/functional-test-failing/172875/5 "2009-07-29T16:23:33Z")

</div>

On Jul 30, 12:18 am, shusseina [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 30, 2009, 8:28am UTC](https://www.ruby-forum.com/t/functional-test-failing/172875/6 "2009-07-30T08:28:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 31, 2009, 3:59am UTC](https://www.ruby-forum.com/t/functional-test-failing/172875/7 "2009-07-31T03:59:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 30, 2009, 8:47am UTC](https://www.ruby-forum.com/t/functional-test-failing/172875/8 "2009-07-30T08:47:54Z")

</div>

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

> **[Testing Rails Applications — Ruby on Rails Guides](https://guides.rubyonrails.org/testing.html)**
>
> Testing Rails ApplicationsThis guide covers built-in mechanisms in Rails for testing your application.After reading this guide, you will know: Rails testing terminology. How to write unit, functional, integration, and system tests for your...

Sijo
