how to reset expectations on a mocked class method?
This worked for me to unmock a specific method from a class:
mock = RSpec::Mocks.space.proxy_for(MyClass)
mock.instance_variable_get(:@method_doubles)[:my_method].reset
Note: Same logic of
RSpec::Mocks.space.proxy_for(MyClass).reset
which resets all methods
I could not find anywhere in the documentation that this is how you should do it, and past behaviors goes to show that this solution might also change in the future, but apparently this is how you can currently do it:
RSpec::Mocks.space.proxy_for(your_object).reset
I would follow @BroiSatse's remark, though, and think about re-designing the tests, aiming to move the expectation from the before
block. The before
block is meant for setup, as you say, and the setup is a very weird place to put expectations.
I'm not sure how you came to this design, but I can suggest two possible alternatives:
If the test is trivial, and will work anyway, you should create one test with this explicit expectation, while stubbing it for the other tests:
before(:each) do allow(File).to receive(:exist?).with("dummy.yaml").and_return (true) end it "asks if file exists" do expect(File).to receive(:exist?).with("dummy.yaml").and_return (true) # do the test... end
If the expectation should run for every test, since what changes in each scenario is the context, you should consider using shared examples:
shared_examples "looking for dummy.yaml" do it "asks if file exists" do expect(File).to receive(:exist?).with("dummy.yaml").and_return (true) # do the test... end end it_behaves_like "looking for dummy.yaml" do let(:scenario) { "something which sets the context"} end
You might also want to ask myron if there is a more recommended/documented solution to reset mocked objects...