rspec rails - How to test service object methods are called? -


i'm trying build tests service objects.

my service file follows...

class exampleservice    def initialize(location)     @location = coordinates(location)   end    private    def coordinates(location)     address.locate(location)   end  end 

i want test private methods called public methods. code...

subject { exampleservice.new("london") }  "receives location"   expect(subject).to receive(:coordinates)   subject end 

but error...

expected: 1 time arguments received: 0 times arguments 

in first example, subject has been instantiated/initialized (by being passed expect, invoking coordinates in process) time you've set expectations on it, there no way expectation receive :coordinates succeed. also, aside, subject memoized, there won't additional instantiation in line follows.

if want make sure initialization calls particular method, use following:

describe   subject { foursquareservice.new("london") }   "receives coordinates"     expect_any_instance_of(foursquareservice).to receive(:coordinates)     subject   end end 

see rails / rspec: how test #initialize method?


Comments