3

我已经看到使用旧should_receive语法完成此操作,但我无法弄清楚如何在 Rspec 3 中存根 Rails 模块方法。

我试过这些:

OptionsHelper.stubs(:method).returns("something")
allow(OptionsHelper).to receive(:method).and_return "something"
allow_any_instance_of(ActionView::Base).to receive(:render_quick_help).and_return 'something'

但实际上没有人存根该方法。

4

1 回答 1

1
allow_any_instance_of(ActionView::Base)

意味着它将期望该确切类的实例而不是从它继承的类来接收:方法。

ActionView::Base 也是一个类,ActionView:: 是一个模块。

此外,模块不能被实例化,所以理论上 allow_any_instance_of(ActionView).to ... 不会有任何效果。

我知道这不是解决方案,但应该为您指明正确的方向。

于 2014-08-25T14:38:02.660 回答