有没有办法用Rspec存根包含模块的方法?

use*_*153 29 ruby rspec mixins stubbing

我有一个包含在另一个模块中的模块,它们都实现了相同的方法.我想存根包含模块的方法,如下所示:

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
Run Code Online (Sandbox Code Playgroud)

第一个测试是通过,但第二个测试输出:

Failure/Error: expect(A.foo).to eq :bar

   expected: :bar
        got: :M
Run Code Online (Sandbox Code Playgroud)

为什么在这种情况下存根不工作?有没有不同的方法来实现这一目标?

谢谢!

------------------------------------- UPDATE ------------ ----------------------

谢谢!使用allow_any_instance_of(M)解决了这个问题.我的下一个问题是 - 如果我使用prepend而不包括会发生什么?请参阅以下代码:

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 
Run Code Online (Sandbox Code Playgroud)

这次,使用allow_any_instance_of(M)会导致无限循环.这是为什么?

mde*_*lin 32

注意你不能直接打电话M.foo!您的代码似乎只能起作用,因为您嘲笑M.foo要返回:bar.

当你打开Ametaclass(class << self)来包含时M,你必须模拟M添加到你的before块的任何实例:

allow_any_instance_of(M).to receive(:foo).and_return(:bar)

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 do
    allow(M).to receive(:foo).and_return(:bar)
    allow_any_instance_of(M).to receive(:foo).and_return(:bar)
  end


  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
Run Code Online (Sandbox Code Playgroud)

  • 我认为这只是帮助我解决了 5 个多小时的头撞桌子的问题。谢谢! (2认同)
  • 参见,`expect_any_instance_of` ......这让我走上正轨 (2认同)