有没有办法撤消Test :: Unit中any_instance的Mocha存根 [英] Is there a way to undo Mocha stubbing of any_instance in Test::Unit

查看:82
本文介绍了有没有办法撤消Test :: Unit中any_instance的Mocha存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很像这个问题,我也使用Ryan Bates的nifty_scaffold.使用Mocha的 any_instance 方法来强制使用无效状态隐藏在控制器后面的模型对象中.

Much like this question, I too am using Ryan Bates's nifty_scaffold. It has the desirable aspect of using Mocha's any_instance method to force an "invalid" state in model objects buried behind the controller.

与我链接的问题不同,我不是在使用RSpec,而是在Test :: Unit.这意味着那里的两个以RSpec为中心的解决方案对我不起作用.

Unlike the question I linked to, I'm not using RSpec, but Test::Unit. That means that the two RSpec-centric solutions there won't work for me.

是否有一种通用的方法(即:与Test :: Unit一起使用)来删除any_instance存根?我相信这会导致我的测试出现错误,我想对此进行验证.

Is there a general (ie: works with Test::Unit) way to remove the any_instance stubbing? I believe that it's causing a bug in my tests, and I'd like to verify that.

推荐答案

碰巧,Mocha 0.10.0允许

As it happens, Mocha 0.10.0 allows 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!"

这篇关于有没有办法撤消Test :: Unit中any_instance的Mocha存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆