Rails RR 框架:多次调用instance_of
我想使用 RR 为我的控制器编写 RSpec。
我编写了以下代码:
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe RegistrationController do
it "should work" do
#deploy and approve are member functions
stub.instance_of(Registration).approve { true }
stub.instance_of(Registration).deploy { true }
post :register
end
end
但是,当仍然调用原始 approve 方法时,RR 仅存根 deploy 方法。
我应该使用什么语法来对 Registration 类的所有实例的两个方法调用进行存根?
更新: 我用[Mocha]达到了预期的结果
Registration.any_instance.stubs(:deploy).returns(true)
Registration.any_instance.stubs(:approve).returns(true)
I would like write RSpec for my controller using RR.
I wrote following code:
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe RegistrationController do
it "should work" do
#deploy and approve are member functions
stub.instance_of(Registration).approve { true }
stub.instance_of(Registration).deploy { true }
post :register
end
end
However RR stubs only deploy method when still calls original approve method.
What syntax should I use to stub both method calls for all instances of Registration class?
UPDATE:
I achivied desired result with [Mocha]
Registration.any_instance.stubs(:deploy).returns(true)
Registration.any_instance.stubs(:approve).returns(true)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,RSpec 模拟不允许你这样做。 您确定需要存根所有实例吗? 我通常遵循这种模式:
通过存根您控制的 Registration 类的 find 方法,在规范中返回什么对象。
as far as I know, the RSpec mocks don't allow you to do that. Are you sure, that you need to stub all instances? I usually follow this pattern:
By stubbing the find method of the Registration class you control, what object gets returned in the spec.
看来您描述的行为实际上是一个错误:
http://github.com/ btakita/rr/issues#issue/17
It would appear the behavior you describe is actually a bug:
http://github.com/btakita/rr/issues#issue/17