有没有办法撤消 Mocha 对any_instance 的存根?
在我的控制器规格中,我是否有效?对于一些路由测试,(基于 Ryan Bates nifty_scaffold)如下:-
it "create action should render new template when model is invalid" do
Company.any_instance.stubs(:valid?).returns(false)
post :create
response.should render_template(:new)
end
当我单独测试控制器时,这很好。我的模型规范中也有以下内容,
it "is valid with valid attributes" do
@company.should be_valid
end
这在单独测试时效果很好。如果我为模型和控制器运行规范,就会出现问题。模型测试总是失败为有效?方法已被淘汰。当控制器测试被拆除时,有没有办法让我删除any_instance 的存根。
我通过按相反的字母顺序运行测试来解决这个问题,以确保模型测试在控制器之前运行,但我真的不喜欢我的测试依赖于序列。
Within my controller specs I am stubbing out valid? for some routing tests, (based on Ryan Bates nifty_scaffold) as follows :-
it "create action should render new template when model is invalid" do
Company.any_instance.stubs(:valid?).returns(false)
post :create
response.should render_template(:new)
end
This is fine when I test the controllers in isolation. I also have the following in my model spec
it "is valid with valid attributes" do
@company.should be_valid
end
Again this works fine when tested in isolation. The problem comes if I run spec for both models and controllers. The model test always fails as the valid? method has been stubbed out. Is there a way for me to remove the stubbing of any_instance when the controller test is torn down.
I have got around the problem by running the tests in reverse alphabetic sequence to ensure the model tests run before the controllers but I really don't like my tests being sequence dependant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要手动配置RSpec。
另外,请记住 Rspec 提供了自己的方法来模拟对象。使用 RSpec API,否则您将无法从库抽象中受益。
http://rspec.info/documentation/mocks/message_expectations.html
You need to manually configure RSpec.
Also, remember that Rspec provides its own methods to mock an object. Use the RSpec API or you won't be able to benefit from library abstraction.
http://rspec.info/documentation/mocks/message_expectations.html
我遇到了同样的问题,但没有使用
Rspec
,而是使用正常的Test::Unit
,实际上是ActionController::TestCase
。定义的期望在测试中保持活力。
有什么线索可以让我在测试之间重置我的期望吗?
更新:我已经用
unstub
Mocha 方法解决了这个问题:http://mocha.rubyforge.org/classes/Mocha/ObjectMethods.html#M000009I'm having the same issue but not using
Rspec
but normalTest::Unit
, actuallyActionController::TestCase
.The defined expectations are keeping alive among the tests.
Is there any clue how can I reset my expectations between tests?
Updated: I have solved this with the
unstub
Mocha method: http://mocha.rubyforge.org/classes/Mocha/ObjectMethods.html#M000009你的spec_helper
是否包含rspec应该在测试之间拆除模拟。
Does your spec_helper contain
With that rspec should tear down mocks between tests.