Rails测试失败,但系统正常工作

时间:2013-10-09 21:09:46

标签: ruby-on-rails testing tdd

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

update_counter功能运行良好并且报告正确,但测试失败。 @prayer变量是指向数据库还是指向某个单独的fixture对象?我也试过@prayer = Prayer.find(1),但结果相同。有什么想法吗?

Fixture yml是:

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

感谢。

1 个答案:

答案 0 :(得分:1)

您可以尝试reload方法:

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

这导致Rails重新加载@prayer从DB获取实际数据。