Rails:Cucumber 发送邮件两次
这应该是一个很容易追踪的...但对我来说并没有证明这一点:
我有以下黄瓜场景:
Scenario: Send mail
Given I am a guest
When I go to the new_contact page
And I fill in "contact_name" with "Test User"
And get mail count
And I fill in "contact_email" with "[email protected]"
And I fill in "contact_message" with "Test Message"
And I fill in "contact_phone_num" with "123456789"
And I press "Send Message"
And get mail count
除了“获取邮件计数”之外的所有默认步骤,它只是返回:
puts ActionMailer::Base.deliveries.count
“的第一步” get mail count”返回零,第二个返回 2。运行 ActionMailer::Base.deliveries 确认电子邮件是相同的(包括对象标识符)。我一生都无法弄清楚第二个发送来自哪里。实际使用该应用程序时,邮件只会发送一次。相关代码如下:
控制器:
class ContactsController < ApplicationController
def new
@contact = Contact.new
@pagetitle = "Contact Us"
if (current_user) then
@contact.name = "#{current_user.first_name} #{current_user.last_name}"
@contact.email = current_user.email
end
end
def create
@contact = Contact.new(params[:contact])
if @contact.save
contactmailer = ContactMailer
puts 'here now'
contactmailer.contact_message(@contact).deliver
redirect_to contact_thanks_url, notice: 'Contact was successfully created.'
else
render action: "new"
end
end
def thanks
end
end
邮件程序:
class ContactMailer < ActionMailer::Base
def contact_message(contact)
@contact = contact
mail(:to => ENV['MANAGER_EMAIL'], :from => @contact.email, :subject => t(:business_name), :content_type => 'text/plain')
end
end
Cucumber 配置文件:
BC::Application.configure do
require 'ruby-debug'
config.cache_classes = true
config.use_transactional_fixtures = true
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"
config.whiny_nils = true
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_dispatch.show_exceptions = false
config.action_controller.allow_forgery_protection = false
config.action_mailer.delivery_method = :test
config.active_support.deprecation = :stderr
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
ENV['MANAGER_EMAIL'] = '[email protected]'
end
This should be an easy one to track down...but it isn't proving that way for me:
I have the following cucumber scenario:
Scenario: Send mail
Given I am a guest
When I go to the new_contact page
And I fill in "contact_name" with "Test User"
And get mail count
And I fill in "contact_email" with "[email protected]"
And I fill in "contact_message" with "Test Message"
And I fill in "contact_phone_num" with "123456789"
And I press "Send Message"
And get mail count
All default steps except for "get mail count", which simply returns:
puts ActionMailer::Base.deliveries.count
The first step of "get mail count" returns zero, the second returns 2. Running ActionMailer::Base.deliveries confirms the email is identical (including object identifier). I cannot, for the life of me, figure out where that second send is coming from. When actually using the app, the mail only comes through once. Relevant code below:
Controller:
class ContactsController < ApplicationController
def new
@contact = Contact.new
@pagetitle = "Contact Us"
if (current_user) then
@contact.name = "#{current_user.first_name} #{current_user.last_name}"
@contact.email = current_user.email
end
end
def create
@contact = Contact.new(params[:contact])
if @contact.save
contactmailer = ContactMailer
puts 'here now'
contactmailer.contact_message(@contact).deliver
redirect_to contact_thanks_url, notice: 'Contact was successfully created.'
else
render action: "new"
end
end
def thanks
end
end
Mailer:
class ContactMailer < ActionMailer::Base
def contact_message(contact)
@contact = contact
mail(:to => ENV['MANAGER_EMAIL'], :from => @contact.email, :subject => t(:business_name), :content_type => 'text/plain')
end
end
Cucumber Config File:
BC::Application.configure do
require 'ruby-debug'
config.cache_classes = true
config.use_transactional_fixtures = true
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"
config.whiny_nils = true
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_dispatch.show_exceptions = false
config.action_controller.allow_forgery_protection = false
config.action_mailer.delivery_method = :test
config.active_support.deprecation = :stderr
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
ENV['MANAGER_EMAIL'] = '[email protected]'
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果有人有同样的问题,请回答:
email_spec gem。 support/features/env.rb 中的“require”语句重复调用了邮件程序。为什么我不确定,但我卸载了 gem &一切都很好。
Answer in case anyone has the same issue:
email_spec gem. The 'require' statement in support/features/env.rb was double-calling the mailer. Why I'm not sure, but I uninstalled the gem & everything worked fine.