【问题标题】:`stub` for Class in rspec2rspec2中类的`stub`
【发布时间】:2012-08-14 15:28:44
【问题描述】:
describe SomeThing do
  before :all do
    # ...
    FooClass.stub(:fooMethod).with('a').and_return("something")
  end
end

rspec 1 合作很酷。

我已经更新到rspec 2,这就是我现在收到的这条线路:

Failure/Error: 
   FooClass.stub(:fooMethod).with('a').and_return("something")
     NoMethodError:
       undefined method `stub' for FooClass::Class

rspec api 却说:Person.stub(:find) { person }

我错过了什么?

【问题讨论】:

  • 你使用什么命令来运行规范?

标签: ruby rspec


【解决方案1】:

这是在 itbefore 块内吗?

before :all 块中不支持存根。在每个示例之后,存根和模拟都会被清除。您可以阅读更多关于此here 的信息。将before :all do 更改为before do,这应该可以工作。

【讨论】:

  • describe ... before :all do块中
  • 哎呀,对不起。应该在您的代码中看到这一点..您可以将代码移动到 it 或将 before :all do ... end 更改为简单的 before do ... end 吗?
  • 好像before :all do -> before do 解决了这个问题,还不太确定
  • 您使用before :all do 有什么原因吗?确保您清楚 before allbefore 的区别。很高兴它现在可以工作了。
  • 是的,beforebefore :each 的别名。 before :all 块中不支持存根。在每个示例之后,存根和模拟都会被清除。您可以通过以下方式了解更多信息:github.com/rspec/rspec-mocks/issues/92
猜你喜欢
  • 1970-01-01
  • 2017-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多