我有一个被包含在另一个模块中的模块,它们都实现了相同的方法。 我想要对被包含模块的方法进行存根处理,就像这样:
module M
def foo
:M
end
end
module A
class << self
include M
def foo
super
end
end
end
describe "trying to stub the included method" do
before { allow(M).to receive(:foo).and_return(:bar) }
it "should be stubbed when calling M" do
expect(M.foo).to eq :bar
end
it "should be stubbed when calling A" do
expect(A.foo).to eq :bar
end
end
第一次测试通过了,但第二次测试输出:
Failure/Error: expect(A.foo).to eq :bar
expected: :bar
got: :M
在这种情况下为什么存根(stub)不起作用?还有其他实现方式吗?
谢谢!
-------------------------------------更新----------------------------------
谢谢!使用 allow_any_instance_of(M) 解决了这个问题。我的下一个问题是,如果我使用 prepend 而不是 include 会发生什么?请看下面的代码:
module M
def foo
super
end
end
module A
class << self
prepend M
def foo
:A
end
end
end
describe "trying to stub the included method" do
before { allow_any_instance_of(M).to receive(:foo).and_return(:bar) }
it "should be stubbed when calling A" do
expect(A.foo).to eq :bar
end
end
这一次,使用 allow_any_instance_of(M) 会导致无限循环。为什么会这样?
allow
不同,allow_any_instance_of
不要求方法在对象上定义。 - Peter Ehrlichexpect_any_instance_of
... 这让我找对了方向。 - Daniel Bidulock