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变量是指向数据库还是指向单独的夹具对象?我也尝试过@prayer = Prayer.find(1),但结果相同。有任何想法吗?

夹具yml是:

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


谢谢。

最佳答案

您可以尝试reload方法:

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


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

关于ruby-on-rails - Rails测试失败,但系统正常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19282788/

10-14 12:50