# Functional test behaving oddly in a manually working model

**URL:** <https://www.ruby-forum.com/t/functional-test-behaving-oddly-in-a-manually-working-model/151876>\
**Category:** Rails\
**Created:** [October 27, 2008, 11:33am UTC](https://www.ruby-forum.com/t/functional-test-behaving-oddly-in-a-manually-working-model/151876 "2008-10-27T11:33:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [October 27, 2008, 11:33am UTC](https://www.ruby-forum.com/t/functional-test-behaving-oddly-in-a-manually-working-model/151876/1 "2008-10-27T11:33:17Z")

</div>

Hello all,-

wondering if you can help me with a weird testing problem. I have a  
Newsletter model that has been developed and tested in a previous  
version (older branch) of my current project. Under that old project,  
all tests run perfectly with no failures. Moreover, in the current  
project Newsletter behaves perfectly with manual tests - ie creating,  
deleting, editing newsletters by hand.

When I run rake test:functionals however, the tests don’t do what they  
are supposed to. Here’s a method from NewslettersControllerTest:

def test\_should\_create\_newsletter  
puts “TESTING CREATE”  
login\_as(:admin\_user) #method in test\_helper.rb  
puts "I am " + users(:admin\_user).id.to\_s #loads correct user from  
fixture!  
puts "My email: " + users(:admin\_user).email #same, correct user  
old\_count = Newsletter.count  
puts "Old count: " + old\_count.to\_s #1  
post :create, :newsletter =\> { :subject =\> ‘testing creating  
newsletter’, :body =\> ‘Test’ }  
puts "Old count after create: " + old\_count.to\_s #1  
#assert\_equal old\_count+1, Newsletter.count #fails  
puts "Newsletter count after create: " + Newsletter.count.to\_s #1  
AGAIN!  
#assert\_redirected\_to newsletter\_path(assigns(:newsletter))  
end

And here’s the console output:

Started  
…TESTING CREATE  
I am 2  
My email: removed\_email\_address@domain.invalid  
Old count: 1  
Old count after create: 1  
Newsletter count after create: 1  
.FFFFFF…  
Finished in 3.518293 seconds.

1. Failure:  
test\_should\_destroy\_newsletter(NewslettersControllerTest)  
[/Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active\_support/core\_ext/test/unit/assertions.rb:43:in  
`assert_difference' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/routing/route.rb:48:in`each\_with\_index’  
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active\_support/core\_ext/test/unit/assertions.rb:42:in  
`each' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active_support/core_ext/test/unit/assertions.rb:42:in`each\_with\_index’  
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active\_support/core\_ext/test/unit/assertions.rb:42:in  
`assert_difference' ./test/functional/newsletters_controller_test.rb:58:in`test\_should\_destroy\_newsletter’  
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active\_support/testing/setup\_and\_teardown.rb:33:in  
` __send__' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in`run’]:  
\<0\> expected but was  
\<1\>.

Can anyone see what is wrong here?

Many thanks, Vahagn

---

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [October 27, 2008, 11:39am UTC](https://www.ruby-forum.com/t/functional-test-behaving-oddly-in-a-manually-working-model/151876/2 "2008-10-27T11:39:12Z")

</div>

On Oct 27, 10:33 am, Vahagn H. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> are supposed to. Here’s a method from NewslettersControllerTest:  
> Have you tried looking a little closer at why its failing ? Eg stick a  
> breakpoint in the action and see what happens. If the action doesn’t  
> run at all because of some filter (eg authentication - are you sure  
> your login\_as method works ?) then there will be a note in test.log

Fred

---

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [October 27, 2008, 11:39am UTC](https://www.ruby-forum.com/t/functional-test-behaving-oddly-in-a-manually-working-model/151876/3 "2008-10-27T11:39:19Z")

</div>

PS!

The “Failure” console output above is for the wrong test (as the  
assertion was commented out in test\_create\_newsletter). Here’s the right  
output:

1. Failure:  
test\_should\_create\_newsletter(NewslettersControllerTest)  
[./test/functional/newsletters\_controller\_test.rb:33:in  
`test_should_create_newsletter' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in` **send** ’  
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active\_support/testing/setup\_and\_teardown.rb:33:in  
`run’]:  
\<2\> expected but was  
\<1\>.

/ Vahagn

Vahagn H. wrote:

> Hello all,-
> 
> wondering if you can help me with a weird testing problem. I have a  
> Newsletter model that has been developed and tested in a previous  
> version (older branch) of my current project. Under that old project,  
> all tests run perfectly with no failures. Moreover, in the current  
> project Newsletter behaves perfectly with manual tests - ie creating,  
> deleting, editing newsletters by hand.
> 
> When I run rake test:functionals however, the tests don’t do what they  
> are supposed to. Here’s a method from NewslettersControllerTest:
> 
> def test\_should\_create\_newsletter  
> puts “TESTING CREATE”  
> login\_as(:admin\_user) #method in test\_helper.rb  
> puts "I am " + users(:admin\_user).id.to\_s #loads correct user from  
> fixture!  
> puts "My email: " + users(:admin\_user).email #same, correct user  
> old\_count = Newsletter.count  
> puts "Old count: " + old\_count.to\_s #1  
> post :create, :newsletter =\> { :subject =\> ‘testing creating  
> newsletter’, :body =\> ‘Test’ }  
> puts "Old count after create: " + old\_count.to\_s #1  
> #assert\_equal old\_count+1, Newsletter.count #fails  
> puts "Newsletter count after create: " + Newsletter.count.to\_s #1  
> AGAIN!  
> #assert\_redirected\_to newsletter\_path(assigns(:newsletter))  
> end
> 
> And here’s the console output:
> 
> Started  
> …TESTING CREATE  
> I am 2  
> My email: removed\_email\_address@domain.invalid  
> Old count: 1  
> Old count after create: 1  
> Newsletter count after create: 1  
> .FFFFFF…  
> Finished in 3.518293 seconds.
> 
> 1. Failure:  
> test\_should\_destroy\_newsletter(NewslettersControllerTest)  
> [/Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active\_support/core\_ext/test/unit/assertions.rb:43:in  
> `assert_difference' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/routing/route.rb:48:in`each\_with\_index’  
> /Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active\_support/core\_ext/test/unit/assertions.rb:42:in  
> `each' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active_support/core_ext/test/unit/assertions.rb:42:in`each\_with\_index’  
> /Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active\_support/core\_ext/test/unit/assertions.rb:42:in  
> `assert_difference' ./test/functional/newsletters_controller_test.rb:58:in`test\_should\_destroy\_newsletter’  
> /Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active\_support/testing/setup\_and\_teardown.rb:33:in  
> ` __send__' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in`run’]:  
> \<0\> expected but was  
> \<1\>.
> 
> Can anyone see what is wrong here?
> 
> Many thanks, Vahagn

---

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [October 27, 2008, 12:15pm UTC](https://www.ruby-forum.com/t/functional-test-behaving-oddly-in-a-manually-working-model/151876/4 "2008-10-27T12:15:39Z")

</div>

On 27 Oct 2008, at 10:43, Vahagn H. wrote:

> Hi Fred,-
> 
> login\_as works in all other tests! And I actually test it by the puts  
> statements, it works because I can see the admin\_user’s id is 2, and  
> email is removed\_email\_address@domain.invalid. This means that login\_as works by loading  
> that user from the fixture:

Fair enough (although you’re not actually testing that it works, just  
that that fixture exists, which is different from whether the session  
has been faked up properly or something like that).  
I’d still check your logs to see if some filter or something is  
stopping your action from running. Another possibility is a validation  
failure.

Fred

---

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [October 27, 2008, 11:43am UTC](https://www.ruby-forum.com/t/functional-test-behaving-oddly-in-a-manually-working-model/151876/5 "2008-10-27T11:43:03Z")

</div>

Hi Fred,-

login\_as works in all other tests! And I actually test it by the puts  
statements, it works because I can see the admin\_user’s id is 2, and  
email is removed\_email\_address@domain.invalid. This means that login\_as works by loading  
that user from the fixture:

admin\_user:  
id: 2  
username: admin  
email: removed\_email\_address@domain.invalid  
hashed\_password: #very long hash  
created\_at: \<%= 1.days.ago.to\_s(:db) %\>

/ Vahagn

Frederick C. wrote:

> On Oct 27, 10:33ï¿½am, Vahagn H. \<rails-mailing-l…@andreas-  
> [s.net](http://s.net)\> wrote:
> 
> > are supposed to. Here’s a method from NewslettersControllerTest:  
> > Have you tried looking a little closer at why its failing ? Eg stick a  
> > breakpoint in the action and see what happens. If the action doesn’t  
> > run at all because of some filter (eg authentication - are you sure  
> > your login\_as method works ?) then there will be a note in test.log
> 
> Fred

---

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [October 27, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/functional-test-behaving-oddly-in-a-manually-working-model/151876/6 "2008-10-27T12:33:22Z")

</div>

SOLVED!

Fred, thanks a ton. There was another fixture, roles\_users, that I  
forgot to import from the old project. Which resulted in the test  
skipping the create method because of invalid user authentication.

I arrived at that by placing a breakpoint in the controller (create),  
and by examining the test log (great tip, sure to become a habit!)

All the best,  
Vahagn

Frederick C. wrote:

> On 27 Oct 2008, at 10:43, Vahagn H. wrote:
> 
> > Hi Fred,-
> > 
> > login\_as works in all other tests! And I actually test it by the puts  
> > statements, it works because I can see the admin\_user’s id is 2, and  
> > email is removed\_email\_address@domain.invalid. This means that login\_as works by loading  
> > that user from the fixture:
> 
> Fair enough (although you’re not actually testing that it works, just  
> that that fixture exists, which is different from whether the session  
> has been faked up properly or something like that).  
> I’d still check your logs to see if some filter or something is  
> stopping your action from running. Another possibility is a validation  
> failure.
> 
> Fred
