cod*_*red 2 rspec devise capybara ruby-on-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:
#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)
Run Code Online (Sandbox Code Playgroud)
当用户填写完表格并单击“注册”按钮后,页面将重定向到“关于”页面,并按预期方式出现以下即时消息:“带有确认链接的消息已发送到您的电子邮件地址。请请点击链接激活您的帐户。”
使用save_and_open_page,我确认了上述行为。但是,该规范失败,并出现以下错误:
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)>'
Run Code Online (Sandbox Code Playgroud)
该错误表明ActionMailer :: Base.deliveries数组中没有消息对象。Pry的结果确认ActionMailer :: Base.deliveries数组确实为空:
[1] pry(main)> mail = ActionMailer::Base.deliveries
=> []
Run Code Online (Sandbox Code Playgroud)
这是运行规范时的测试日志:
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)
Run Code Online (Sandbox Code Playgroud)
该日志表明已发送确认电子邮件,但是传递数组为空。为什么会这样呢?记录由于某种原因未提交或未保留在测试数据库中吗?我已经阅读了有关未送达邮件的相关文章,但是没有找到解决我问题的方法。
来自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
Run Code Online (Sandbox Code Playgroud)
来自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
Run Code Online (Sandbox Code Playgroud)
来自devise.rb的相关注释掉的代码:
# Configure the class responsible to send e-mails.
#config.mailer = 'Devise::Mailer'
Run Code Online (Sandbox Code Playgroud)
邮件投递在开发和生产中按预期工作。在测试环境中出了什么问题?我该如何解决?谢谢!
看着色器件- https://github.com/plataformatec/devise/blob/4-1-stable/lib/devise/models/confirmable.rb#L48 -它发出的确认邮件中的after_commit回调-如果你在启用了事务测试的情况下运行,after_commit回调将永远不会被调用(因为数据库事务已回滚并且从未提交),因此电子邮件从不发送。为该测试禁用基于事务的测试,它可能会起作用。
| 归档时间: |
|
| 查看次数: |
1233 次 |
| 最近记录: |