What actually needs to be different in each example? It seems like
you're using the same @user each time, so that could be expressed in
the shared example e.g.
shared_examples "sends all types" do
it 'sends an email' do
FactoryGirl.create(:email_notification,
notification_type: notif_type, user: FactoryGirl.create(:user))
notification_email_with_expects(resource, expected_str)
end
end
You can also move the notification type to the shared example - just
passing in the name used to look up the type:
shared_examples "sends all types" do |notification_type_name|
it 'sends an email' do
FactoryGirl.create(:email_notification,
notification_type: NotificationType.find_by_name(notification_type_name),
user: FactoryGirl.create(:user))
notification_email_with_expects(resource, expected_str)
end
end
describe 'Something Bad Happens' do
it_behaves_like "sends all types", "bad stuff" do
let(:resource) { @company2 }
let(:expected_str) { @
company2.name }
end
end
Then it seems like @company2 can be declared within the example as well:
shared_examples "sends all types" do |notification_type_name|
it 'sends an email' do
company = FactoryGirl.create(:company)
FactoryGirl.create(:email_notification,
notification_type: NotificationType.find_by_name(notification_type_name),
user: FactoryGirl.create(:user))
notification_email_with_expects(company,
company.name)
end
end
describe 'Something Bad Happens' do
it_behaves_like "sends all types", "bad stuff"
end
I'm sure I'm missing some things, but you get the idea.
Cheers,
David