我也知道这些存根/模拟被用于允许孤立的测试,如控制器中没有触摸模型.但是,我仍然对这两种方法感到困惑,当时每个方法都被使用?细节和例子将非常感谢.非常感谢!
解决方法
The
stub_model
method generates an instance of a Active Model model.While you can use
stub_model
in any example (model,view,controller,helper),it is especially useful in view examples,which are inherently more state-based than interaction-based.
mock_model
The
mock_model
method generates a test double that acts like an Active Model
model. This is different from thestub_model
method which generates an instance of a real ActiveModel class.The benefit of
mock_model
overstub_model
is that its a true double,so the examples are not dependent on the behavIoUr (or mis-behavIoUr),or even the existence of any other code.
If you’re working on a controller spec and you need a model that doesn’t exist,you can passmock_model
a string and the generated object will act as though its an instance of the class named by that string.
rspec docs:stub_model和mock_model