【问题标题】:FactoryGirl problems with has_one and belongs_to associationshas_one 和 belongs_to 关联的 FactoryGirl 问题
【发布时间】:2012-12-09 12:27:24
【问题描述】:

我已经与我的协会斗争了 3 天,不知道还能去哪里。我确信问题很简单,但我对 Ruby on Rails 还很陌生,这让我很困惑......

我创建了一个用户模型,它包含所有用于设计身份验证的登录凭据。我有另一个 Profile 模型,其中包含所有用户的设置(名字等)。最后,我有一个地址模型,它使用与配置文件关联的多态关联。

用户has_one 个人资料。个人资料belongs_to 用户和has_one 地址。 Address 是一种多态关联,它使我的应用程序中的其他模型能够拥有与之关联的地址。

在某一时刻,我的所有 FactoryGirl 定义都可以正常工作,但我正在对 accepts_nested_attributes_for 问题进行故障排除,并添加了一个 after_initialize 回调来构建用户的配置文件和配置文件的地址。现在我的工厂之间有一个循环引用,我的 rspec 输出充满了:

stack level too deep

由于过去几天我对配置进行了如此多的修改,我觉得最好停下来寻求帮助。 :) 这就是我在这里的原因。如果有人能帮我解决这个问题,我将不胜感激。

这是我的出厂配置:

用户工厂

FactoryGirl.define do
  sequence(:email) {|n| "person-#{n}@example.com"}
  factory :user do
    profile
    name 'Test User'
    email 
    password 'secret'
    password_confirmation 'secret'
    # required if the Devise Confirmable module is used
    confirmed_at Time.now
  end
end

配置文件工厂

FactoryGirl.define do
  factory :profile do
    address
    company_name "My Company"
    first_name "First"
    last_name "Last"
  end
end

地址工厂

FactoryGirl.define do
  factory :address do
    association :addressable, factory: :profile
    address "123 Anywhere"
    city "Cooltown"
    state "CO"
    zip "12345"
    phone "(123) 555-1234"
    url "http://mysite.com"
    longitude 1.2
    latitude 9.99
  end
end

理想情况下,我希望能够相互独立地测试每个工厂。在我的用户模型测试中,我希望有一个像这样的有效工厂:

describe "user"
  it "should have a valid factory" do
    FactoryGirl.create(:user).should be_valid
  end
end

describe "profile"
  it "should have a valid factory" do
    FactoryGirl.create(:profile).should be_valid
  end
end

describe "address"
  it "should have a valid factory" do
    FactoryGirl.create(:address).should be_valid
  end
end

秘诀是什么?我查看了 Factory Girl 的 wiki 和整个网络,但我担心我在搜索中没有使用正确的术语。此外,在我偶然发现的每个搜索结果中,似乎有 4 种不同的方法可以在 FactoryGirl 中使用混合语法来完成所有操作。

提前感谢您的任何见解...

更新:2012 年 12 月 26 日

我的个人资料/用户关联倒退了。我没有让 User 引用 Profile 工厂,而是翻转它让 Profile 引用 User 工厂。

这是最终的工厂实现:

用户工厂

FactoryGirl.define do
  sequence(:email) {|n| "person-#{n}@example.com"}
  factory :user do
    #profile <== REMOVED THIS!
    name 'Test User'
    email 
    password 'please'
    password_confirmation 'please'
    # required if the Devise Confirmable module is used
    confirmed_at Time.now
  end
end

配置文件工厂

FactoryGirl.define do
  factory :profile do
    user # <== ADDED THIS!
    company_name "My Company"
    first_name "First"
    last_name "Last"
  end
end

地址工厂

FactoryGirl.define do
  factory :address do
    user
    association :addressable, factory: :profile
    address "123 Anywhere"
    city "Cooltown"
    state "CO"
    zip "90210"
    phone "(123) 555-1234"
    url "http://mysite.com"
    longitude 1.2
    latitude 9.99
  end
end

所有测试都通过了!

【问题讨论】:

  • 您的用户工厂中的单行profile 是做什么用的?我假设您正在尝试将其链接到创建的 FactoryGirl 配置文件?
  • 你是对的。我正在尝试使用预构建的配置文件创建用户。原因是在我对用户的after_initialize 方法中,我构建了一个配置文件,但是当我保存它时,由于配置文件值空白,验证不允许用户保存......?
  • @jason328 你应该发布你的答案,我会给你信用。你的评论是解决办法! :) 非常感谢!
  • @luc 为了清楚起见,您能否发布最终(固定)用户模型?

标签: rspec ruby-on-rails-3.2 factory-bot


【解决方案1】:

根据提问者的要求,

您将profile 留空。为了关联用户和个人资料,您需要填写该行的其余部分并让 FactoryGirl 知道用户和个人资料已关联。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    相关资源
    最近更新 更多