Reputation: 15
I'm looking to test that a class method calls a specific instance method. Is there any way to do this? This is the best I've got, but it fails.
describe '#foo' do
let(:job) { create :job }
it 'calls job.bar' do
job.should_receive(:bar)
Job.foo
end
end
I need to be sure that the right instance of job is called, not just any instance. I appreciate any help.
Upvotes: 1
Views: 3809
Reputation: 6818
You can use stubs on the method by which .foo
gets the instance.
For instance:
describe '.foo' do
let(:job) { create :job }
it 'calls job.bar' do
Job.stub(:find).and_return job
job.should_receive(:bar)
Job.foo
end
end
What this does is ensures that the instance that you expect to have methods called on is the one that actually gets used by .foo
.
You can add expectations or argument matchers to this, so:
Job.should_receive(:find).with(job.id).and_return(job)
Upvotes: 4