所以问题是邮件没有被添加到ActionMailer :: Base.deliveries。
FactoryGirl.create返回零。我做错了什么?
FactoryGirl.define do
factory :provisional_user do
sequence(:email) { |n| "bangbang_#{n}@example.com" }
first_name "Provisional"
last_name "User"
partner "source2"
unsubscribed false
end
factory(:unsubscribed_user, :parent => :provisional_user, :class => ProvisionalUser) do
sequence(:email) { |n| "do_not_contact@example.com" }
first_name "Unsubscribed"
last_name "User"
partner "source2"
unsubscribed true
end
factory(:subscribed_user, :class => ProvisionalUser) do
sequence(:email) { |n| "please_contact@example.com" }
first_name "Subscribed"
last_name "User"
partner "source2"
unsubscribed false
end
...
end
然后在我的测试中(我也尝试了FactoryGirl.create,没有保存!在下一行):
require "rspec"
require "spec_helper"
require "action_mailer"
describe "unsubscribe functionality" do
before(:each) do
ActionMailer::Base.deliveries = []
end
it "should send emails to subscribed users only" do
unsubscribed_user = FactoryGirl.build(:unsubscribed_user)
unsubscribed_user.save!
subscribed_user = FactoryGirl.create(:subscribed_user)
puts "the user is" + subscribed_user.to_s
CoRegEmailWorker.perform
#sent.length.should == 1
sent.first.email.should =~ subscribed_user.email
sent.first.email.should_not =~ unsubscribed_user.email
end
def sent
ActionMailer::Base.deliveries
end
end
但它失败了:
Failure/Error: sent.first.email.should =~ subscribed_user.email
NoMethodError:
undefined method `email' for nil:NilClass
# ./spec/mailers/provisional_users_notifier_spec.rb:21
答案 0 :(得分:1)
在你放在这里的代码中,你只定义了unsubscribed_user,但是没有subscribed_user,所以subscribed_user将是nil,并且可能存在问题。
答案 1 :(得分:0)
您的用户类是否具有一对一或一对多关联?我有一个类似的错误消息,并通过包含关联创建我的工厂对象解决它。这很有帮助:
How to create has_and_belongs_to_many associations in Factory girl