spec/lib/sendgrid_actionmailer_spec.rb in sendgrid-actionmailer-2.0.0 vs spec/lib/sendgrid_actionmailer_spec.rb in sendgrid-actionmailer-2.0.1

- old
+ new

@@ -57,10 +57,12 @@ end end describe '#deliver!' do let(:client) { TestClient.new } + let(:client_parent) { double(client: client) } + let(:mail) do Mail.new( to: 'test@sendgrid.com', from: 'taco@cat.limo', subject: 'Hello, world!', @@ -69,9 +71,40 @@ before do stub_request(:any, 'https://api.sendgrid.com/api/mail.send.json') .to_return(body: {message: 'success'}.to_json, status: 200, headers: {'X-TEST' => 'yes'}) allow(SendGrid::Client).to receive(:new).and_return(client) + allow(SendGrid::API).to receive(:new).and_return(client_parent) + end + + context 'with dynamic api_key' do + let(:default) do + Mail.new( + to: 'test@sendgrid.com', + from: 'taco@cat.limo', + subject: 'Hello, world!' + ) + end + + let(:mail) do + Mail.new( + to: 'test@sendgrid.com', + from: 'taco@cat.limo', + subject: 'Hello, world!', + delivery_method_options: { + api_key: 'test_key' + } + ) + end + + it 'sets dynamic api_key, but should revert to default settings api_key' do + expect(SendGrid::API).to receive(:new).with(api_key: 'key') + mailer.deliver!(default) + expect(SendGrid::API).to receive(:new).with(api_key: 'test_key') + mailer.deliver!(mail) + expect(SendGrid::API).to receive(:new).with(api_key: 'key') + mailer.deliver!(default) + end end it 'sets to' do mailer.deliver!(mail) expect(client.sent_mail['personalizations'][0]).to include({"to"=>[{"email"=>"test@sendgrid.com"}]})