很像this question ,我也在使用 Ryan Bates 的 nifty_scaffold。它具有使用 Mocha 的 any_instance 的理想方面。在 Controller 后面的模型对象中强制进入“无效”状态的方法。
与我链接到的问题不同,我没有使用 RSpec,而是使用 Test::Unit。这意味着那里的两个以 RSpec 为中心的解决方案对我不起作用。
是否有通用的(即:与 Test::Unit 一起使用)删除 any_instance stub 的方法?我认为它导致我的测试出现错误,我想验证这一点。
最佳答案
碰巧,Mocha 0.10.0 允许 unstubbing on any_instance() .
str = "Not Stubbed!"
String.any_instance.stubs(:to_s).returns("Stubbed!")
puts str.to_s # "Stubbed!"
String.any_instance.unstub(:to_s)
puts str.to_s # "Not Stubbed!"
关于ruby - 有没有办法在 Test::Unit 中撤消 any_instance 的摩卡 stub ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2894331/