1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
Feature: `have_enqueued_mail` matcher
The `have_enqueued_mail` (also aliased as `enqueue_mail`) matcher is used to check if given mailer was enqueued.
Background:
Given active job is available
Scenario: Checking mailer class and method name
Given a file named "spec/mailers/user_mailer_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe NotificationsMailer do
it "matches with enqueued mailer" do
ActiveJob::Base.queue_adapter = :test
expect {
NotificationsMailer.signup.deliver_later
}.to have_enqueued_mail(NotificationsMailer, :signup)
end
end
"""
When I run `rspec spec/mailers/user_mailer_spec.rb`
Then the examples should all pass
Scenario: Checking mailer enqueued time
Given a file named "spec/mailers/user_mailer_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe NotificationsMailer do
it "matches with enqueued mailer" do
ActiveJob::Base.queue_adapter = :test
expect {
NotificationsMailer.signup.deliver_later(wait_until: Date.tomorrow.noon)
}.to have_enqueued_mail.at(Date.tomorrow.noon)
end
end
"""
When I run `rspec spec/mailers/user_mailer_spec.rb`
Then the examples should all pass
Scenario: Checking mailer arguments
Given a file named "app/mailers/my_mailer.rb" with:
"""ruby
class MyMailer < ApplicationMailer
def signup(user = nil)
@user = user
mail to: "to@example.org"
end
end
"""
Given a file named "spec/mailers/my_mailer_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe MyMailer do
it "matches with enqueued mailer" do
ActiveJob::Base.queue_adapter = :test
# Works with plain args
expect {
MyMailer.signup('user').deliver_later
}.to have_enqueued_mail(MyMailer, :signup).with('user')
end
end
"""
When I run `rspec spec/mailers/my_mailer_spec.rb`
Then the examples should all pass
Scenario: Parameterize the mailer
Given a file named "app/mailers/my_mailer.rb" with:
"""ruby
class MyMailer < ApplicationMailer
def signup
@foo = params[:foo]
mail to: "to@example.org"
end
end
"""
Given a file named "spec/mailers/my_mailer_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe MyMailer do
it "matches with enqueued mailer" do
ActiveJob::Base.queue_adapter = :test
# Works with named parameters
expect {
MyMailer.with(foo: 'bar').signup.deliver_later
}.to have_enqueued_mail(MyMailer, :signup).with(a_hash_including(params: {foo: 'bar'}))
end
end
"""
When I run `rspec spec/mailers/my_mailer_spec.rb`
Then the examples should all pass
Scenario: Parameterize and pass an argument to the mailer
Given a file named "app/mailers/my_mailer.rb" with:
"""ruby
class MyMailer < ApplicationMailer
def signup(user)
@user = user
@foo = params[:foo]
mail to: "to@example.org"
end
end
"""
Given a file named "spec/mailers/my_mailer_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe MyMailer do
it "matches with enqueued mailer" do
ActiveJob::Base.queue_adapter = :test
# Works also with both, named parameters match first argument
expect {
MyMailer.with(foo: 'bar').signup('user').deliver_later
}.to have_enqueued_mail(MyMailer, :signup).with(params: {foo: 'bar'}, args: ['user'])
end
end
"""
When I run `rspec spec/mailers/my_mailer_spec.rb`
Then the examples should all pass
|