简体   繁体   English

使用Amazon ses邮件发送时Rspec测试失败

[英]Rspec tests failing when using amazon ses mail sending

I recently switched over a rails app to send mail through amazon ses. 我最近切换了Rails应用程序,以通过Amazon ses发送邮件。 The sending of the mail works fine, however the tests are failing around the mail items with the following error: 邮件发送工作正常,但是围绕邮件项目的测试失败,并出现以下错误:

TypeError: can't convert Fixnum into String # (eval):3:in 'send_raw_email'

I have the test environment mail settings set properly to :test 我已将测试环境邮件设置正确设置为:test

config.action_mailer.delivery_method = :test

I can prevent the errors from happening by not sending emails, but then I get errors about the mail not being delivered. 我可以通过不发送电子邮件来防止发生错误,但是我会收到有关未送达邮件的错误。

config.action_mailer.perform_deliveries = false
should_change -> { ActionMailer::Base.deliveries.size } do
   result should have changed, but is still 0

Gem versions are below: 宝石版本如下:

rails (3.2.12)
rspec (2.8.0)
cucumber (2.3.3)
aws-sdk (1.33.0)

Any suggestions on what is going on, or how to work around this. 关于正在发生的事情或如何解决此问题的任何建议。 If any other info is needed let me know. 如果需要其他任何信息,请告诉我。 This worked fine before I switched the sending of mail to use amazon ses. 在我将邮件发送切换为使用Amazon ses之前,此方法工作正常。

EDIT: 编辑:

The tests that are failing, are all that involve an email being sent using .deliver 失败的测试全部涉及使用.deliver发送的电子邮件

BlahMailer.send_invite(team).deliver

This is inside a model and controller which the tests just call the associated route in the controller or call the associated function in the model. 它在模型和控制器内部,测试仅在控制器中调用关联的路由或在模型中调用关联的函数。 Not sure what else to show, since the error is showing its coming from send_raw_email which is the ses function, not any code from the app. 不知道还要显示什么,因为错误显示的是来自ses函数的send_raw_email,而不是应用程序中的任何代码。

EDIT 2: More code 编辑2:更多代码

Here is one of the tests, in this example it sets up the factory for the model and each test. 这是测试之一,在本示例中,它为模型和每个测试设置了工厂。

before :each do
  subject = Factory(:ticket, action_type: 'name_change', team_name: 'test123')
end

Here is the factory in rspec 这是rspec的工厂

Factory.define :ticket do |ticket|
  team = nil
  ticket.team { team = Factory(:team) }
  ticket.creator { team.captain }
  ticket.subject { Factory(:team_assignment, team: team, user: Factory(:active_user)).user }
end

And then here is the code in the model around where the email is sent 这是模型中围绕电子邮件发送位置的代码

%w(requested approved cancelled accepted).each do |state|
  define_method "notify_#{state}" do
    TicketMailer.send("#{action_type}_#{state}", self).deliver
  end
end

I have tried putting in straight text for basically everything having to do with the mailer, just to make sure there are no issues with that. 我已经尝试为与邮件程序有关的所有事情都输入纯文本,以确保没有任何问题。 If you need anymore of the model or the actual mailer let me know. 如果您需要模型或实际的邮递员,请告诉我。 I just really cant figure out where this is dieing. 我只是真的不知道这是哪里死了。 My only conclusion is its just a bug with the older versions of the aws sdk and rspec gems. 我唯一的结论是,这只是旧版aws sdk和rspec gem的错误。 I even tried replacing aws-sdk with newer versions but was still unsuccessful in getting anything else to work. 我什至尝试用较新的版本替换aws-sdk,但仍然无法成功进行其他工作。

The problem is that you're setting config.action_mailer.perform_deliveries = false , so no deliveries are being made and therefore ActionMailer::Base.deliveries will always be an empty collection. 问题是您要设置config.action_mailer.perform_deliveries = false ,因此不会进行任何传递,因此ActionMailer::Base.deliveries将始终是一个空集合。

If you want to test email deliveries, then set config.action_mailer.perform_deliveries = true and use an interceptor to ensure you're not sending emails to users. 如果要测试电子邮件的传递,请设置config.action_mailer.perform_deliveries = true并使用拦截器来确保不向用户发送电子邮件。

lass SandboxEmailInterceptor
  def self.delivering_email(message)
    recipients =
      if Rails.env.test?
        [
          "user@example.com"
        ]
      end

    message.to = recipients
    message.cc = []
  end
end

unless Rails.env.production? || Rails.env.test?
  ActionMailer::Base.register_interceptor(SandboxEmailInterceptor)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM