ruby on rails - Mocha: stubbing method with specific parameter but not for other parameters -
i want stub method mocha when specific parameter value given , call original method when other value given.
when this:
myclass.any_instance.stubs(:show?).with(:wanne_show).returns(true) i
unexpected invocation myclass.show?(:other_value) i know, can stub parameters when writing mock without ´with´-call , give specific mock. have know return value every call, not case :/
tldr; there way call original method in stub or stub specific parameters , leave others?
the answer depends on you're testing.
a few notes:
1) avoid using stubs.any_instance. can specific in stubs/mocks, prevents false testing positives.
2) prefer using spies along stubs, actively assert has been called. use bourne gem purpose. alternative use mock, implicitly tests if being called (e.g. fail if doesn't called.
so class-method might looks (note, rspec syntax):
require 'bourne' require 'mocha' 'calls myclass.show?(method_params)' myclass.stubs(:show?) anotherclass.method_which_calls_my_class expect(myclass).to have_received(:show?).with('parameter!') end class anotherclass def self.method_which_calls_my_class myclass.show?('parameter!') end end there lot of stub/spy examples here.
hope helps.
Comments
Post a Comment