Ruby 中的模拟:模拟在测试之间持续存在
我使用 RR 作为我个人项目的模拟框架。我一直在使用它来模拟某些类的新方法,当我运行测试时,它们通过得很好,但是当我运行所有测试时,我遇到了一个问题,似乎“新”方法仍然返回即使在不同的测试文件中,结果也是假的。有没有办法手动关闭新方法的存根?我在这里缺少什么?
感谢您的帮助, 亚历克斯(Alex)
我尝试将此代码放入我的应用程序中,但它破坏了应用程序,并且......并没有解决上述问题。
RSpec.configure do |config|
config.mock_with :rr
end
I'm using RR as the mocking framework for a personal project of mine. I've been using it to mock the new method for some classes and when I run the tests they pass fine, but when I run ALL of the tests I run into a problem where it seems like the "new" methods are still returning the fake results, even when in a different test file. Is there a way to turn off the stubbing of the new method manually? What am I missing here?
Thanks for the help,
Alex
I've tried putting this code into my app and it breaks the app, and.... doesn't fix the above problem.
RSpec.configure do |config|
config.mock_with :rr
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模拟正在取代您的旧方法。一旦某个方法被您的模拟替换,除非您采取预防措施,否则它将永远不会再正常。
看看
Mocks are replacing your old methods. Once a method is replaced by your mock it will never be normal again unless you using precautions.
Take a look at this thread. I explained there how one can undo Mocha mocks using simple
alias
.RSpec 的模拟会在测试之间自动拆除。但是,当您将 RSpec 配置为使用另一个库进行模拟时,您仍然必须通过 RSpec 模拟 API 使用它。
RSpec's mocks are torn down automatically between tests. However, when you configure RSpec to mock with another library, you still have to use it through the RSpec mocking API.