If you MyService dependency inside your object, you can replace it with a layout without the methods defined on it, so any method call will throw an exception.
Let me show you an example:
class Manager attr_reader :service def initialize(service = MyService) @service = service end def do_stuff service.do_stuff end def tested_method other_stuff end end
And the tests will be:
context "#do_stuff" do let(:manager) { Manager.new } it 'invokes MyService by default' do MyService.should_receive(:do_stuff) manager.do_stuff end end context "#tested_method" do let(:service) { mock("FakeService") } let(:manager) { Manager.new(service) } it 'does not invoke any service' do expect { manager.tested_method }.not_to raise_error end end
source share