在Rspec 3中Rspec any_instance的新等价物是什么?

Sar*_*els 19 testing rspec ruby-on-rails

通常在我的Rails控制器测试中,我会before { Website.any_instance.stub(:save).and_return(false) }测试当记录不保存时会发生什么.它似乎any_instance与Rspec 3一起消失了.

我尝试使用before { allow(Website).to receive(:save).and_return(false) }Rspec 3,但现在我收到此错误:

网站(id:整数,...)未实现:save

是否有替代any_instanceRspec 3 非常有用?

ush*_*sha 34

试试这个

allow_any_instance_of(Website).to receive(:save).and_return(false)
Run Code Online (Sandbox Code Playgroud)

例如:https://github.com/rspec/rspec-mocks#settings-mocks-or-stubs-on-any-instance-of-a-class