Gibt es eine Möglichkeit, eine Methode eines in Rspec enthaltenen Moduls zu entfernen?
Ich habe ein Modul, das in einem anderen Modul enthalten ist, und beide implementieren die gleiche Methode. Ich möchte die Methode des enthaltenen Moduls in etwa wie folgt abkoppeln:
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
Der erste Test ist erfolgreich, der zweite gibt jedoch Folgendes aus:
Failure/Error: expect(A.foo).to eq :bar
expected: :bar
got: :M
Warum funktioniert der Stub in diesem Fall nicht? Gibt es einen anderen Weg, um dies zu erreichen?
Vielen Dank!
-------------------------------------AKTUALISIEREN------------ ----------------------
Vielen Dank! Mit allow_any_instance_of (M) wurde dieses Problem gelöst. Meine nächste Frage ist - was passiert, wenn ich prepend verwende und nicht include? siehe folgenden Code:
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
Diesmal führt die Verwendung von allow_any_instance_of (M) zu einer Endlosschleife. warum ist das so?