Есть ли способ заглушить метод включенного модуля с Rspec?

У меня есть модуль, который включен в другой модуль, и они оба реализуют один и тот же метод. Я хотел бы заглушить метод включенного модуля, что-то вроде этого:

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

Почему заглушка не работает в этом случае? Есть ли другой способ добиться этого?

Спасибо!

-------------------------------------ОБНОВИТЬ------------ ----------------------

Спасибо! использование 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 

На этот раз использование allow_any_instance_of (M) приводит к бесконечному циклу. это почему?

Ответы на вопрос(1)

Ваш ответ на вопрос