2013-10-09 54 views
1
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),但结果相同。有任何想法吗?Rails测试失败,但系统工作

夹具阳明是:

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

感谢。

+0

仅供参考,[它是不好的形式](http://rest.elkstein.org/2008/02/more-complex-rest-requests.html)通过GET请求在服务器上引起更改。 – Dogweather

+0

谢谢。现在更改为'发布'(除非你坚持),我想我在尝试解决一些集成测试时得到'get'。 – folium

回答

1

你可以尝试reload方法:

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

这个原因Rails的从数据库加载@prayer获取的实际数据。

+0

哇!惊人!非常感谢! – folium

相关问题