邮件发送器示例中的 URL 助手

邮件发送器规格由 type: :mailer 标记,或者如果您已将 config.infer_spec_type_from_file_location! 设置为将它们放置在 spec/mailers 中。

使用具有默认选项的 URL 助手

假定有一个名为“config/initializers/mailer_defaults.rb”的文件,其中包含

Rails.configuration.action_mailer.default_url_options = { :host => 'example.com' }

以及一个名为“spec/mailers/notifications_spec.rb”的文件,其中包含

require 'rails_helper'

RSpec.describe NotificationsMailer, type: :mailer do
  it 'should have access to URL helpers' do
    expect { gadgets_url }.not_to raise_error
  end
end

我运行 rspec spec

那么这些示例应该全部通过。

使用没有默认选项的 URL 助手

假定有一个名为“config/initializers/mailer_defaults.rb”的文件,其中包含

# no default options

以及一个名为“spec/mailers/notifications_spec.rb”的文件,其中包含

require 'rails_helper'

RSpec.describe NotificationsMailer, type: :mailer do
  it 'should have access to URL helpers' do
    expect { gadgets_url :host => 'example.com' }.not_to raise_error
    expect { gadgets_url }.to raise_error
  end
end

我运行 rspec spec

那么这些示例应该全部通过。