Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reset expectations on a mocked class method?

Sorry if this is plain simple. i am new to ruby as well as rspec and it seems rspec is a very 'obscure' world (esp when coming from a .net background).

In my 'spec', i have:

before(:each) do
    expect(File).to receive(:exist?).with("dummy.yaml").and_return (true)
end

This works fine for all my 'examples', except one where i want it to return false.

expect(File).to receive(:exist?).with("non_existent.yaml").and_return (false)

This obviously fails my test because although "non_existent.yaml" expectation was met, the "dummy.yaml" was not:

(<File (class)>).exist?("dummy.yaml")
       expected: 1 time with arguments: ("dummy.yaml")
       received: 0 times

So how can i do a 'Reset' on 'File.exist?' (a class method mock) before i setup the new expectation for it? (... "non_existent.yaml"..)

i googled and it yielded:

RSpec::Mocks.proxy_for(your_object).reset

but this gives me:

NoMethodError:
   undefined method `proxy_for' for RSpec::Mocks:Module
like image 916
Bhavneet Singh Bajwa Avatar asked Aug 14 '14 13:08

Bhavneet Singh Bajwa


2 Answers

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...

like image 90
Uri Agassi Avatar answered Nov 14 '22 22:11

Uri Agassi


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

like image 44
Owen Peredo Diaz Avatar answered Nov 14 '22 23:11

Owen Peredo Diaz