![](/img/trans.png)
[英]class method_1 doesn't pass the test, but it works in rails console
[英]Rails Test::Unit does not work. But app works, console works, even console in Test::Unit works
我有一個方法可以簡單地更新屬性。 :
@objects.each do |thingamabobber|
thingamabobber.update_attributes( { attribute_one: false, attribute_two: true } )
end
它做的還多一點,但是在大多數情況下就是這樣。 我將調試器放在該更新部分之前和之后。 而且我注意到在運行Unit :: Test時,它不會更新屬性。
但是,如果在同一調試器中,只需將代碼復制並粘貼到Test :: Unit控制台中,它將更新而不會出現錯誤。 如果我使用Rails控制台,或者如果我整體使用該應用程序,那將是正確的。 一切正常。 除了測試。
注意,我的測試對象中沒有任何東西。 我正在使用Factory girl創建我的對象。
此外,如果我將調試器投入測試中,並在要更新的屬性上運行一個集合,它們將返回無變化 , 但仍通過測試 ,也就是說,除了確保該attribute_one的測試外已更新。
這告訴我..Test :: Unit正在靜默處理數據,並且它失敗了,但不是由於它告訴我的任何原因。
有人知道這可能是什么症狀嗎?
更新資料
這真的很簡單:
should "update all items" do
@item.items.last.update_all_recurring_items( {"name" => 'Ooooohhhh Yeeeeeea! Kooolaid Goooooood!'} )
assert_equal @item.name, @item.items.last.name
end
然后在我的模型中的方法:
def update_all_recurring_items params = {}
parent = self.recurring_items.present? ? self : Item.find(self.recurring_item_id)
parent.recurring_items.each do |item|
# Everything ok up to here. If I run the below method it works.
item.update_attributes( params )
# But! The Test Suit just skips over this like nothing ever happened.
end
parent.update_attributes(params) if self != parent
end
我意識到答案是重新加載對象,以便測試實例化該對象。
所以只需這樣做:
@object.reload
允許所有測試通過,並且控制台可以反映Test :: Unit中的更改。
不過,我很好奇為什么會這樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.