I have to write a spec for the following method:
def track_event
tracker = set_tracker
category = define_event_category
tracker.event(category: category, action: name, label: properties['short_name'], value: properties['paid'])
end
To be specific I have to to check if method event
is called on the variable tracker
when track_event
is called. I tried to do it like this:
describe '#track_event' do
it 'should call event method' do
expect(tracker).to receive(:event)
end
end
and got an error undefined local variable or method 'tracker'
. What do I do wrong?
You cannot access tracker
because it is a local variable.
You could stub your set_tracker
method instead and return an instance double:
describe '#track_event' do
let(:tracker_double) { instance_double(Tracker) }
it 'should call event method' do
allow(foo).to receive(:set_tracker).and_return(tracker_double)
expect(tracker_double).to receive(:event)
foo.track_event
end
end
foo
is your test subject.