所以我用RSpec和email_spec
gem测试电子邮件,我不断遇到这个错误,这让我没有真正的线索来了解导致它的原因。
我对RSpec和一般测试都很陌生。当我用Google搜索时发现错误,我发现one post类似于此错误以及错误上的brief documentation page(这对我来说非常模糊)。
我认为原因是email_spec
可能过时的宝石或方法,但我只是 spec ulating(你的时间有一个双关语)。
这是我一直收到的错误(我在此帖子上编辑了我的电子邮件):
1) UserMailer it sends an activation email when the user signs up should be delivered from XXXXXXXX@gmail.com
Failure/Error: it { should deliver_from "XXXXXXXX@gmail.com" }
NoMethodError:
undefined method `addrs' for #<Mail::UnstructuredField:0x007fc5c680bf18>
# ./spec/mailers/user_mailer_spec.rb:12:in `block (3 levels) in <top (required)>'
很少有事情需要指出:
addrs
方法(所以我相信)Email::Helpers
文件中加入了Email::Matchers
和config/test.rb
。这是我的UserMailer测试:
require "spec_helper"
require "email_spec"
describe UserMailer do
describe "it sends an activation email when the user signs up" do
user = FactoryGirl.build(:user)
subject { UserMailer.activate_account(user) }
it { should deliver_to user.email }
# Error seems to be occurring from the "deliver_from" method
it { should deliver_from "XXXXXXX2@gmail.com" }
it { should have_subject "Please confirm to activate your account." }
it { should have_body_text "Hi, #{user.first_name}" }
it { should have_body_text "Confirm Your Account" }
end
端
我希望得到帮助,并指出正确的方向是什么导致了这个问题,所以我可以采取适当的步骤来解决它。
修改
根据要求添加了回溯。
# /Library/Ruby/Gems/2.0.0/gems/mail-2.6.3/lib/mail/field.rb:189:in `method_missing'
# /Library/Ruby/Gems/2.0.0/gems/email_spec-1.6.0/lib/email_spec/matchers.rb:92:in `block in matches?'
# /Library/Ruby/Gems/2.0.0/gems/email_spec-1.6.0/lib/email_spec/matchers.rb:6:in `address_array'
# /Library/Ruby/Gems/2.0.0/gems/email_spec-1.6.0/lib/email_spec/matchers.rb:92:in `matches?'
# /Library/Ruby/Gems/2.0.0/gems/rspec-expectations-2.99.2/lib/rspec/expectations/handler.rb:24:in `handle_matcher'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/memoized_helpers.rb:77:in `should'
# ./spec/mailers/user_mailer_spec.rb:12:in `block (3 levels) in <top (required)>'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:116:in `block in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:178:in `call'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:178:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:241:in `instance_eval_with_args'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/hooks.rb:108:in `call'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/hooks.rb:108:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/hooks.rb:446:in `run_hook'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:463:in `run_around_each_hooks'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:250:in `with_around_each_hooks'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:113:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:515:in `block in run_examples'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:511:in `map'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:511:in `run_examples'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:496:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:497:in `block in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:497:in `map'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:497:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:24:in `block (2 levels) in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:24:in `map'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:24:in `block in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/reporter.rb:58:in `report'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:21:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:103:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:17:in `block in autorun'
答案 0 :(得分:1)
出色!我弄清楚了。
问题出现在我的UserMailer
课程中(/app/mailers/user_mailer.rb
)。
顶部的default from:
语句与我的测试不匹配。
class UserMailer < ApplicationMailer
default from: "XXXXXXXX2@gmail.com" # <= This was the culprit
def activate_account(user)
@user = user
mail(:to => @user.email, :subject => "Please confirm to activate your account.")
end
end
答案 1 :(得分:0)
我最近碰到了这个问题,我的问题是我在:to
字段中没有提供正确的电子邮件地址
我错了 -
mail(to: @user)
而不是
mail(to: @user.email)