folium
folium

Reputation: 413

Rails test fails, but the system works

setup do
    @prayer = prayers(:one)
end

test "should update prayer count" do
    assert_difference('@prayer.count', 1) do
        get "update_counter", {:id => @prayer.id}
    end
    assert_redirected_to root_path
end

# Running tests:

counter updated and saved. Count is: 
1
F

Finished tests in 1.374664s, 0.7275 tests/s, 0.7275 assertions/s.

  1) Failure:
PrayersControllerTest#test_should_update_prayer_count [test/controllers/prayers_controller_test.rb:147]:
"@prayer.count" didn't change by 1.
Expected: 1
  Actual: 0

The update_counter feature is working well and reporting correctly, but the test fails. Is the @prayer variable pointing to the database, or to some separate fixture object? I have also tried @prayer = Prayer.find(1), but same result. Any ideas?

Fixture yml is:

one:
  id: 1
  prayer: Pray for nice weather
  count: 0
  user_id: 1

Thanks.

Upvotes: 1

Views: 76

Answers (1)

hedgesky
hedgesky

Reputation: 3311

You could try reload method:

assert_difference('@prayer.reload.count', 1) do

This cause Rails to reload @prayer getting actual data from DB.

Upvotes: 1

Related Questions