FactoryGirl与has_one和belongs_to关联有问题

Luc*_*Luc 6 rspec ruby-on-rails-3.2 factory-bot

我和我的协会一直在争吵3天,不知道还有什么地方可以转.我确定问题很简单,但我对Ruby on Rails相当新,这让我感到难过......

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

用户has_one档案.配置文件belongs_to用户和has_one地址.Address是一个多态关联,它使我的应用程序中的其他模型具有与之关联的地址.

有一次,我的所有FactoryGirl定义都有效,但我正在解决accepts_nested_attributes_for问题,并添加了一个after_initialize回调来构建用户的配置文件和配置文件的地址.现在我的工厂彼此有一个循环引用,我的rspec输出充满了:

stack level too deep
Run Code Online (Sandbox Code Playgroud)

由于我在过去几天里对配置进行了如此多的修改,我觉得最好停下来寻求帮助.:)这就是我在这里的原因.如果有人可以帮我这个,我真的很感激.

这是我的工厂配置:

用户工厂

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
Run Code Online (Sandbox Code Playgroud)

简介工厂

FactoryGirl.define do
  factory :profile do
    address
    company_name "My Company"
    first_name "First"
    last_name "Last"
  end
end
Run Code Online (Sandbox Code Playgroud)

地址工厂

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
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望能够彼此独立地测试每个工厂.在我的用户模型测试中,我想拥有一个如此有效的工厂:

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
Run Code Online (Sandbox Code Playgroud)

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

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

更新:12/26/2012

我倒退了个人资料/用户关联.我没有让用户引用配置文件工厂,而是将其翻转以使配置文件引用用户工厂.

这是最终的工厂实施:

用户工厂

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
Run Code Online (Sandbox Code Playgroud)

简介工厂

FactoryGirl.define do
  factory :profile do
    user # <== ADDED THIS!
    company_name "My Company"
    first_name "First"
    last_name "Last"
  end
end
Run Code Online (Sandbox Code Playgroud)

地址工厂

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
Run Code Online (Sandbox Code Playgroud)

所有测试都通过!

jas*_*328 2

根据提问者的要求,

你留空了profile。为了链接用户和配置文件,您需要填写该行的其余部分,并让 FactoryGirl 知道用户和配置文件已链接。