【问题标题】:what's the new equivalent of Rspec any_instance in Rspec 3?Rspec 3 中 Rspec any_instance 的新等价物是什么?
【发布时间】:2014-06-30 20:32:08
【问题描述】:

通常在我的 Rails 控制器测试中,我会使用before { Website.any_instance.stub(:save).and_return(false) } 来测试当记录没有保存时​​会发生什么。看起来any_instance 与 Rspec 3 一起消失了。

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

Website(id: integer, ...) 没有实现:save

是否可以用 Rspec 3 替换非常有用的 any_instance

【问题讨论】:

    标签: ruby-on-rails testing rspec


    【解决方案1】:

    试试这个

    allow_any_instance_of(Website).to receive(:save).and_return(false)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多