[英]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
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
,以從DB獲取實際數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.