I have different functions to write a Rspec tests for. But the problem is, what I try to test here, has same syntax per each function, so I have to copy paste that "expect" line for each test case. Do you know how I can write that expect assertion once and apply to each test step?
look at "expect(Delayed::Job.count).to eq(1)" In the code below:
it 'is able to send reminder email for submission deadline to signed-up users ' do
mail = DelayedMailer.new(@assignment.id, "submission", @due_at)
Delayed::Job.enqueue(payload_object: mail, priority: 1, run_at: 1.second.from_now)
expect(Delayed::Job.count).to eq(1)
expect(Delayed::Job.last.handler).to include("deadline_type: submission")
expect { mail.perform } .to change { Mailer.deliveries.count } .by(1)
end
it 'is able to send reminder email for review deadline to reviewers ' do
mail = DelayedMailer.new(@assignment.id, "review", @due_at)
Delayed::Job.enqueue(payload_object: mail, priority: 1, run_at: 1.second.from_now)
expect(Delayed::Job.count).to eq(1)
expect(Delayed::Job.last.handler).to include("deadline_type: review")
expect { mail.perform } .to change { Mailer.deliveries.count } .by(1)
end
it 'is able to send reminder email for Metareview deadline to meta-reviewers and team members of the assignment' do
mail = DelayedMailer.new(@assignment.id, "metareview", @due_at)
Delayed::Job.enqueue(payload_object: mail, priority: 1, run_at: 1.second.from_now)
expect(Delayed::Job.count).to eq(1)
expect(Delayed::Job.last.handler).to include("deadline_type: metareview")
expect { mail.perform } .to change { Mailer.deliveries.count } .by(2)
end
You've got two options here:
1) Make a helper method that wraps that logic: https://relishapp.com/rspec/rspec-core/v/3-4/docs/helper-methods/define-helper-methods-in-a-module
The only downside to this one is that you'll have to call the method everywhere you want it.
or
2) Use an after each hook: https://relishapp.com/rspec/rspec-core/v/3-4/docs/helper-methods/define-helper-methods-in-a-module
I don't recommend this one because it will literally run after every spec.