2013-05-06 50 views
0

儘管知道功能在應用程序中有效,但我仍有失敗的測試。我的直覺說,我應該嘗試保存我創建的東西,但我不確定如何在assert_difference塊中執行此操作,因爲它看起來不像新的thing已分配給我可以.save的變量。感謝您提供的任何建議。儘管功能在應用程序中運行,但創建項目測試仍然失敗

測試:

test "should create thing" do 
    assert_difference('thing.count') do 
     post :create, thing: { thing_type_id: @thing.thing_type_id, name: @thing.name} 
    end 

輸出:

1) Failure: 
test_should_create_thing(thingsControllerTest) [C:/../thing_controller_test.rb:20]: 
"thing.count" didn't change by 1. 
<3> expected but was 
<2>. 

回答

2

聽起來像是你可能在你的數據庫有超過狀態剩下一些。我看到expected but was <2>,這意味着你的數據庫中已經有兩個Thing了。

您可以嘗試清除測試之間的數據庫狀態。根據您的數據庫檢查出database_cleaner寶石。

此外,看起來您可能已經創建了該對象,存在@thing。如果是這樣的話,這是按預期工作的。

你可以把控制器的方程通過只是測試正常Thing::create來驗證這一點:

test "creates a new Thing" do 
    assert_difference('Thing.count') do 
    Thing.create thing_type_id: @thing.thing_type_id, name: @thing.name 
    end 
end 
+0

謝謝你解釋的細節。 – 2013-05-06 16:33:45

相關問題