和工厂女孩一起建房遇到麻烦
所以问题是邮件没有被添加到 ActionMailer::Base.deliveries 中。
FactoryGirl.create 返回 nil。我做错了什么?
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| "[email protected]" }
first_name "Unsubscribed"
last_name "User"
partner "source2"
unsubscribed true
end
factory(:subscribed_user, :class => ProvisionalUser) do
sequence(:email) { |n| "[email protected]" }
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
so the issue is that mail is not being added to ActionMailer::Base.deliveries.
FactoryGirl.create is returning nil. what am I doing wrong?
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| "[email protected]" }
first_name "Unsubscribed"
last_name "User"
partner "source2"
unsubscribed true
end
factory(:subscribed_user, :class => ProvisionalUser) do
sequence(:email) { |n| "[email protected]" }
first_name "Subscribed"
last_name "User"
partner "source2"
unsubscribed false
end
...
end
then in my test (i also tried FactoryGirl.create without the save! on the following line):
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
but it's failing like this:
Failure/Error: sent.first.email.should =~ subscribed_user.email
NoMethodError:
undefined method `email' for nil:NilClass
# ./spec/mailers/provisional_users_notifier_spec.rb:21
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您放在这里的代码中,您只定义了unsubscribed_user,但没有定义subscribed_user,因此subscribed_user将为nil,并且可能存在问题。
In the code you put here, you only have defined unsubscribed_user, but no subscribed_user, so subscribed_user would be nil, and there may be the problem.
您的用户类是否具有一对一或一对多关联?我有一个类似的错误消息,并通过包含关联创建我的工厂对象来解决它。这很有帮助:
如何创建 has_and_belongs_to_many工厂女孩的协会
Does your user class have a one-to-one or one-to-many association? I had a similar error message and solved it by creating my factory object by including the association. This was helpful:
How to create has_and_belongs_to_many associations in Factory girl