和工厂女孩一起建房遇到麻烦

发布于 2024-12-26 14:35:11 字数 2043 浏览 0 评论 0原文

所以问题是邮件没有被添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不美如何 2025-01-02 14:35:11

在您放在这里的代码中,您只定义了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.

私藏温柔 2025-01-02 14:35:11

您的用户类是否具有一对一或一对多关联?我有一个类似的错误消息,并通过包含关联创建我的工厂对象来解决它。这很有帮助:

如何创建 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文