Reputation: 313
Let's say there are two classes:
1.
class Fax
def initialize(number)
**code**
end
def send!
**code**
end
end
class FaxJob
def perform
Fax.new(number).send!
end
end
In the FaxJobSpec, I need to confirm that
FaxJob.perform_now(number)
run the Fax.new(number).send!
Upvotes: 0
Views: 1434
Reputation: 721
You should use a double
.
it 'sends fax!' do
fax_instance = instance_double(Fax)
allow(Fax).to(receive(:new).and_return(fax_instance))
allow(fax_instance).to(receive(:send!))
FaxJob.perform_now(number)
expect(fax_instance).to(have_received(:send!))
end
You can avoid having to allow
instance and class with a minor refactor to your Fax
class:
class Fax
def self.send!(number)
new(number).send!
end
end
FaxJob
:
class FaxJob
def perform
Fax.send!(number)
end
end
And then your test:
it 'sends fax!' do
allow(Fax).to(receive(:send!).and_call_original)
FaxJob.perform_now(number)
expect(Fax).to(have_received(:send!).with(number))
end
If you are really into DRY, this should work too:
it 'sends fax!' do
expect(Fax).to(receive(:send!).with(number))
FaxJob.perform_now(number)
end
I am not really found of this latter one because it does not respect the AAA (arrange, act, assert) and it compromises readability, imo.
Upvotes: 1