According to the Rails documentation, the method assert_emails
allows to transform the syntax :
assert_difference ->{ ActionMailer::Base.deliveries.size }, +1 do
post :method_that_should_send_email
end
to
assert_emails 1 do
post :method_that_should_send_email
end
which I find much better, because I don't have to copy paste the long syntax every time.
However I get the following error in my controllers tests when using the above code:
NoMethodError: undefined method `assert_emails'
I assume this is because the method is only available in mailers tests.
Is there a way to make it available to controllers tests too? Is there a good reason why this is not available by default? Is there a better way to test that calling a specific route results in sending a email?
Thanks.
I've just had the same problem when trying to use assert_emails
in a regular test (not an ActionMailer::TestCase
).
The helpers started working when I included ActionMailer::TestHelper
to my test class.
class FooTest < ActiveSupport::TestCase
include ActionMailer::TestHelper
test '...' do
assert_no_emails do
# ...
end
end
end
I believe that should work in your controller tests as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With