ActionMailer无法在测试环境中传递确认电子邮件-Rails 4 [英] ActionMailer not delivering confirmation emails in test environment - Rails 4

查看:72
本文介绍了ActionMailer无法在测试环境中传递确认电子邮件-Rails 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails(4.2.6),Ruby(2.2.4),Devise(4.1.1),Capybara(2.7.1),Capybara-email(2.5.0),Email_spec(2.1.0) ,Rspec(3.4.0)和Postgres(0.18.4)



我将应用程序从Rails 4.1.15升级到4.2.6后,进行了一些认证和验证。注册测试失败。升级之前,所有测试均已正确通过。该代码在开发环境中按预期工作(例如,确认电子邮件已发送并在Rails服务器终端中可见)。未送达电子邮件的问题仅出现在测试环境中。



这是失败的rspec。/spec/features/users/authentification_spec.rb:56:

 #注册用户

访问 /
click_link注册

find(:css, #user_email)。set( tester9@example.com)
find(:css, #user_password)。set( password900)
find(:css, #user_password_confirmation)。set( password900)

期望{
click_button注册
}。来更改{ActionMailer :: Base。 deliveryies.size} .by(1)

当用户填写表单并点击注册按钮,页面将重定向到关于页面,并按预期显示以下即时消息:一条带有确认链接的消息已发送到您的电子邮件地址。请点击该链接以激活您的帐户。

使用save_and_open_page,我确认了上述行为。但是,该规范失败并出现以下错误:

 失败/错误:
期望{
click_button注册
}。更改{ActionMailer :: Base.deliveries.size} .by(1)

预期结果已更改为1,但已更改为0
#./spec/features/users/authentification_spec.rb:56:在< top(必填)>'


该错误表明ActionMailer :: Base.deliveries数组中没有消息对象。 Pry的结果确认ActionMailer :: Base.deliveries数组确实为空:

  [1] pry(main)> ;邮件= ActionMailer :: Base.deliveries 
=> []

以下是运行规范时的测试日志:

 在2016-06-09 16:16:25-0700为127.0.0.1开始POST /用户,由RegistrationsController#create处理为HTML 
参数:{ utf8 =>✓, user => { email => tester9@example.com, password => [FILTERED], password_confirmation => [已过滤]}, commit =>注册}
[1m [35m(0.8ms)[0m SAVEPOINT active_record_1
[1m [36mUser Exists(3.3 ms)[0m [1mSELECT 1作为用户中的用户之一。电子邮件 ='tester9@example.com'限制1 [0m
[1m [35m用户存在(1.9ms)[0m SELECT 1作为一个来自用户的地方,LOWER( users。 email)= LOWER('tester9@example.com')LIMIT 1
[1m [36mSQL(2.1ms)[0m [1mINSERT INTO users (电子邮件,加密密码, signup_as, created_at, updated_at, confirmation_token, confirmation_sent_at)值($ 1,$ 2,$ 3,$ 4,$ 5,$ 6,$ 7)返回 id [ 0m [[电子邮件, tester9@example.com],[加密ed_pa​​ssword, $ 2a $ 04 $ Vruf8j0A.DdOZLPe0qjVp.4PxzdR7sCdLF4FfyAr4dQSxcQAjpAwy],[ created_at, 2016-06-09 23:16:25.095386],[ updated_at, 25.095386],[ confirmation_token, EmY8JyaVAxAfiq7oQ98z],[ confirmation_sent_at, 2016-06-09 23:16:25.097581]]
[1m [35m(0.4ms)[0m发布保存点active_record_1
重定向到http://www.example.com/about
已完成302发现于113ms(ActiveRecord:9.6ms)

该日志表明已发送确认电子邮件,但传递数组为空。为什么会这样呢?记录是否由于某种原因未提交或保留在测试数据库中?我已经阅读了有关未送达邮件的相关文章,但尚未找到解决我问题的方法。



test.rb中的相关代码:

 #告诉Action Mailer不要将电子邮件发送到现实世界。 
#:test传递方法将已发送的电子邮件累积在
#ActionMailer :: Base.deliveries数组中。

config.action_mailer.delivery_method =:test
config.action_mailer.perform_deliveries = true

rails_helper.rb中的相关内容:

 需要'rspec / rails'
需要'devise '
需要'capybara / rails'
需要'database_cleaner'
需要'capybara / poltergeist'
需要'capybara / email / rspec'
需要'email_spec'

#包括Devise测试助手(例如,允许您在测试中使用Devise的 sign_in方法)

config.include Devise :: TestHelpers,:type => :controller
config.include Warden :: Test :: Helpers

相关的注释掉的代码from devise.rb:

 #配置负责发送电子邮件的类。 
#config.mailer ='Devise :: Mailer'

邮件传递在开发与生产。在测试环境中出了什么问题?我该如何解决?谢谢!

解决方案

看着装置- https://github.com/plataformatec/devise/blob/4-1-stable/lib/devise/models/ Confirmable.rb#L48 -它在after_commit回调中发送确认电子邮件-如果在启用事务测试的情况下运行,则after_commit回调将永远不会被调用(因为数据库事务已回滚且从未提交),因此电子邮件永远不会发送。对该测试禁用基于事务的测试,它可能会起作用。


I'm using Rails (4.2.6), Ruby (2.2.4), Devise (4.1.1), Capybara (2.7.1), Capybara-email (2.5.0), Email_spec (2.1.0), Rspec (3.4.0), and Postgres (0.18.4)

After I upgraded the application from Rails 4.1.15 to 4.2.6, several authentification & registration tests fail. Before the upgrade all tests were properly passing. The code works as expected in the development environment (for example, confirmation emails are sent & viewable in the Rails server terminal). The problem of non-delivered emails only occurs in the test environment.

Here's the failing rspec ./spec/features/users/authentification_spec.rb:56:

#Sign up User

    visit "/"
    click_link "Sign up"

    find(:css, "#user_email").set("tester9@example.com")
    find(:css, "#user_password").set("password900")
    find(:css, "#user_password_confirmation").set("password900")

    expect {
            click_button "Sign up"      
        }.to change{ ActionMailer::Base.deliveries.size}.by(1)

When a user completes the form and clicks the "Sign up" button, the page redirects to the "About" page and as expected, the following flash message appears: "A message with a confirmation link has been sent to your email address. Please follow the link to activate your account."
Using save_and_open_page, I confirmed the aforementioned behavior. However, the spec fails with the following error:

Failure/Error:
   expect {
           click_button "Sign up"  
       }.to change{ ActionMailer::Base.deliveries.size}.by(1)

   expected result to have changed by 1, but was changed by 0
 # ./spec/features/users/authentification_spec.rb:56:in `block (2 levels) in <top (required)>'

The error indicates that there are no message objects in the ActionMailer::Base.deliveries array. The results of Pry confirm that the ActionMailer::Base.deliveries array is indeed empty:

[1] pry(main)> mail = ActionMailer::Base.deliveries
=> []

Here's the test log when the spec is run:

Started POST "/users" for 127.0.0.1 at 2016-06-09 16:16:25 -0700
Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "user"=>{"email"=>"tester9@example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
  [1m[35m (0.8ms)[0m  SAVEPOINT active_record_1
  [1m[36mUser Exists (3.3ms)[0m  [1mSELECT  1 AS one FROM "users" WHERE "users"."email" = 'tester9@example.com' LIMIT 1[0m
  [1m[35mUser Exists (1.9ms)[0m  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('tester9@example.com') LIMIT 1
  [1m[36mSQL (2.1ms)[0m  [1mINSERT INTO "users" ("email", "encrypted_password", "signup_as", "created_at", "updated_at", "confirmation_token",     "confirmation_sent_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"[0m  [["email", "tester9@example.com"], ["encrypted_password", "$2a    $04$Vruf8j0A.DdOZLPe0qjVp.4PxzdR7sCdLF4FfyAr4dQSxcQAjpAwy"], ["created_at", "2016-06-09 23:16:25.095386"], ["updated_at", "2016-06-09 23:16:25.095386"],     ["confirmation_token", "EmY8JyaVAxAfiq7oQ98z"], ["confirmation_sent_at", "2016-06-09 23:16:25.097581"]]
  [1m[35m (0.4ms)[0m  RELEASE SAVEPOINT active_record_1
Redirected to http://www.example.com/about
Completed 302 Found in 113ms (ActiveRecord: 9.6ms)

The log indicates that a confirmation email was sent, but the deliveries array is empty. Why is that happening? Is the record not being committed or persisted in the test database for some reason? I've read related posts about non-delivery of mail, but have not found a solution to my problem.

Relevant code from test.rb:

  # Tell Action Mailer not to deliver emails to the real world.
  # The :test delivery method accumulates sent emails in the
  # ActionMailer::Base.deliveries array.  

  config.action_mailer.delivery_method = :test
  config.action_mailer.perform_deliveries = true

Relevant contents from rails_helper.rb:

require 'rspec/rails'
require 'devise'
require 'capybara/rails'
require 'database_cleaner'
require 'capybara/poltergeist'
require 'capybara/email/rspec'
require 'email_spec'

  # Includes Devise test helpers (e.g., lets you use Devise's "sign_in" method in tests)

  config.include Devise::TestHelpers, :type => :controller 
  config.include Warden::Test::Helpers

Relevant commented-out code from devise.rb:

  # Configure the class responsible to send e-mails.
   #config.mailer = 'Devise::Mailer'

Mail delivery works as expected in development & production. What is going wrong in the test environment? How can I fix it? Thanks!

解决方案

Looking at devise - https://github.com/plataformatec/devise/blob/4-1-stable/lib/devise/models/confirmable.rb#L48 - it sends the confirmation email in an after_commit callback - If you're running with transactional testing enabled the after_commit callback will never be called (because the DB transaction is rolled back and never committed) so the email is never sent. Disable transaction based testing for that test and it will probably work.

这篇关于ActionMailer无法在测试环境中传递确认电子邮件-Rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆