Functional test behaving oddly in a manually working model

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: admin@example.com
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

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

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 Hayrapetyan 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 admin@example.com. This means that login_as works by loading
that user from the fixture:

admin_user:
  id: 2
  username: admin
  email: admin@example.com
  hashed_password: #very long hash
  created_at: <%= 1.days.ago.to_s(:db) %>

/ Vahagn

Frederick Cheung 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 admin@example.com. 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

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 Cheung wrote: