New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ActionMailer::Base can unregister observer(s) and interceptor(s). #32207
ActionMailer::Base can unregister observer(s) and interceptor(s). #32207
Conversation
One or multiple mail observers can be unregistered using `ActionMailer::Base.unregister_observers` or `ActionMailer::Base.unregister_observer`. One or multiple mail interceptors can be unregistered using `ActionMailer::Base.unregister_interceptors` or `ActionMailer::Base.unregister_interceptor`. For preview interceptors, it's possible to use `ActionMailer::Base.unregister_preview_interceptors` or `ActionMailer::Base.unregister_preview_interceptor`.
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
actionmailer/test/base_test.rb
Outdated
mail.deliver_now | ||
end | ||
|
||
ActionMailer::Base.unregister_observer(MyObserver) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be called inside the ensure
otherwise if the test fail in the lines above this the observer will still be registered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review. I'll fix it as soon as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rafaelfranca
How about the commit below.
1286c12
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
@kmiyake do you mind adding changeling for this feature as well?
actionmailer/CHANGELOG.md
Outdated
This makes it possible to dynamically add and remove email observers and | ||
interceptors at runtime in the same way they're registered. | ||
|
||
*Kota Miyake* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this builds on #18446, could you add the original author's name here too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this builds on #18446, could you add the original author's name here too?
Fixed.
Done. |
**Only intercept when necessary** This was the original intention but after many attempts realised that the initializers are called before any kind of mocking/stubbing goes on in the RSpec test. So we would never be able to control whether the interceptor is registered within our tests. So next we looked at ignoring the interceptor and using either RSpec filters or hooks to control the registering and unregistering of the interceptor. However we hit an issue there as well. Registering the interceptor was fine, but unregistering was only merged into ActionMailer in May 2018 (rails/rails#32207). So we'd be left with having to also introduce a monkey patch to ensure the tests were clean and the interceptor was removed. 😩 This seemed overkill for what we wanted to achieve. Hence we hit on a final hack, which is to always register the interceptor in the initializer if we are running in "test". **Fixing the test** Realised when working through trying to control whether the interceptor is registered or not that the previous version of the test wasn't actually testing its intent. It is based on the fact that the last email cache feature is enabled, and as such when we check the response we expect to see some email content. However when the interceptor was not being registered `LastEmailCache` was just returning its default error message. As the test was just comparing the response to `LastEmailCache.last_email_json` they were always going to match whether the interceptor was registered or not. So this also tweaks the test such that it matches the intent of checking that the response contains content from an actual sent email.
**Only intercept when necessary** This was the original intention but after many attempts realised that the initializers are called before any kind of mocking/stubbing goes on in the RSpec test. So we would never be able to control whether the interceptor is registered within our tests. So next we looked at ignoring the interceptor and using either RSpec filters or hooks to control the registering and unregistering of the interceptor. However we hit an issue there as well. Registering the interceptor was fine, but unregistering was only merged into ActionMailer in May 2018 (rails/rails#32207). So we'd be left with having to also introduce a monkey patch to ensure the tests were clean and the interceptor was removed. 😩 This seemed overkill for what we wanted to achieve. Hence we hit on a final hack, which is to always register the interceptor in the initializer if we are running in "test". **Fixing the test** Realised when working through trying to control whether the interceptor is registered or not that the previous version of the test wasn't actually testing its intent. It is based on the fact that the last email cache feature is enabled, and as such when we check the response we expect to see some email content. However when the interceptor was not being registered `LastEmailCache` was just returning its default error message. As the test was just comparing the response to `LastEmailCache.last_email_json` they were always going to match whether the interceptor was registered or not. So this also tweaks the test such that it matches the intent of checking that the response contains content from an actual sent email.
Summary
This adds the ability to unregister previously registered observer(s) and interceptor(s).
Other Information
This changes was re-implemented this.