Wilberto
Wilberto

Reputation: 540

Rails test sending emails on request specs

I'm using rails 4.2.4 and I am trying to test that a email is sent in a requests spec.

it "sends a reset password email to the user" do
  expect do
    post users_reset_password_path, params, headers
  end.to change(ActionMailer::Base.deliveries, :size).by(1)
end

This is resulting in:

end.to change(ActionMailer::Base.deliveries, :size).by(1)

expected `Array#size` to have changed by 1, but was changed by 0

I'm sure my email is being sent because I see it in log/test.log. So my questing, is this happening because it is a request spec? If so, how can I test sending an email in a request spec? Is ActionMailer::Base.deliveries only updated in mailer specs?

To provide more context I'm using devise so in the controller I'm calling user.send_reset_password_instructions.

Upvotes: 1

Views: 636

Answers (1)

Wilberto
Wilberto

Reputation: 540

It turns out that my initializers were overriding the configuration in config/environments/test.rb. In mailer specs this is fine because ActionMailer::TestCase forces ActionMailer::Base.delivery_method to be :test. In requests specs this doesn't happen.

Anyways I had to:

diff --git a/config/initializers/mail.rb b/config/initializers/mail.rb
deleted file mode 100644
index 4b8000f..0000000
--- a/config/initializers/mail.rb
+++ /dev/null
@@ -1,10 +0,0 @@
-ActionMailer::Base.smtp_settings = {
-    :address              => "example.com",
-    :port                 => 587,
-  } 
- 
-ActionMailer::Base.delivery_method = :smtp

and add these settings to each config/environment/ file.

Upvotes: 1

Related Questions