简体   繁体   English

使用rails 3中的ActionMailer以测试模式发送电子邮件

[英]Sending emails in test mode with ActionMailer in rails 3

I am having a slightly odd problem with sending mail in test mode with Rails 3 我在使用Rails 3在测试模式下发送邮件时遇到了一个奇怪的问题

It seems that my mailers are not returning anything. 我的邮件似乎没有返回任何东西。 For example I have a mailer called UserMailer. 例如,我有一个名为UserMailer的邮件程序。 Users can make changes that require approval in the app so this has a method called changes_approved that should send the user an email notifying them that their changes have been approved.class 用户可以在应用程序中进行需要批准的更改,这样就会有一个名为changes_approved的方法,该方法应该向用户发送一封电子邮件,通知他们他们的更改已被批准。

UserMailer < ActionMailer::Base

  default :from => "from@example.com"

  def changes_approved(user, page)

    @user = user
    @page = page

    mail(:to => user.email, :subject => "Your changes have been approved")

  end

end

In my controller I have the following line 在我的控制器中,我有以下行

UserMailer.changes_approved(@page_revision.created_by, @page_revision.page).deliver

However my tests fail at this point with the error: 但是我的测试在此时失败并出现错误:

undefined method `deliver' for nil:NilClass 未定义的方法`deliver'代表nil:NilClass

When I trigger the same actions on the development site tho (http://localhost:3000 through a browser), the emails are sent out correctly and everything works quite happily 当我在开发站点上触发相同的操作(http:// localhost:3000通过浏览器)时,电子邮件被正确发送出去,一切都很愉快

And to add further confusion, I am using devise for authentication and the emails for that seem to be working correctly both in test and development modes. 并且为了进一步增加混淆,我正在使用设计进行身份验证,并且电子邮件似乎在测试和开发模式下都能正常工作。 Certainly I am not getting this same error and according to my email-spec tests, everythings working 当然,我没有得到同样的错误,根据我的电子邮件规范测试,每个人都在工作

So this leads me to believe that I have a problem with my mailers rather than my test mail config per se but I have no idea what. 所以这让我相信我的邮件有问题,而不是我的测试邮件配置本身,但我不知道是什么。 Any suggestions would be much appreciated 我们欢迎所有的建议

Thanks 谢谢

I used https://gist.github.com/1031144 to convert 我使用https://gist.github.com/1031144进行转换

# Rails 2 method:
UserMailer.should_receive(:deliver_signup)

to

# Cumbersome Rails 3 method:
mailer = mock
mailer.should_receive(:deliver)
UserMailer.should_receive(:signup).and_return(mailer)

I had a similar problem - probably the UserMailer.changes_approved method is being replaced with a mock method, which returns nil (I wasn't using shoulda for that test, but that's my best guess). 我有一个类似的问题 - 可能是UserMailer.changes_approved方法被替换为模拟方法,它返回nil(我没有使用shoulda进行该测试,但这是我最好的猜测)。

My code looked like this (modified to use your example): 我的代码看起来像这样(修改后使用你的例子):

UserMailer.expects(:changes_approved).once

I fixed it with an additional stub: 我用一个额外的存根修复了它:

@mailer = stub(:deliver)
UserMailer.expects(:changes_approved).once.returns(@mailer)

The nil is now replaced with @mailer. nil现在被@mailer取代。

To test the delayed action mailer we need to first change the configuration of delayed_job (in config/initializers/delayed_job_config.rb) to 要测试延迟的动作邮件程序,我们需要先将delayed_job的配置(在config / initializers / delayed_job_config.rb中)更改为

Delayed::Worker.delay_jobs = !Rails.env.test?

and in your tests the expectation should be set to 在你的测试中,期望应该设置为

mock_mail = mock(:mail)
mock_mail.should_receive(:deliver)
UserMailer.should_receive(:changes_approved).with(user, page).and_return(mock_mail)

Well I have found the answer, 好吧,我找到了答案,

it looks like the problem was in the way I was testing these mailers. 看起来问题就在于我测试这些邮件的方式。 In each of the controller tests I had a line similar to 在每个控制器测试中,我都有类似的线

UserMailer.should_receive(:changes_approved).with(user, page)

Whilst this test was passing fine, it appeared to break the mailer itself. 虽然这个测试通过很好,但似乎打破了邮件本身。 I have removed this line from the tests and now they pass ok. 我从测试中删除了这一行,现在他们通过了确定。 Subsequent tests against ActionMailer::Base.deliveries.last to check the details of the sent email are correct appear to be ok so I am happy that this line is not neccessary. 对ActionMailer :: Base.deliveries.last的后续测试检查发送的电子邮件的详细信息是正确的,所以我很高兴这条线不是必需的。

If anyone has an explanation as to why this breaks tho, I would be interested to find out 如果有人解释为什么这会破坏,我会有兴趣找出来

Thanks anyways 不管怎么说,多谢拉

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

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