[英]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.