私は 3 日間しっかりと自分の団体と戦ってきましたが、他にどこに頼ればよいかわかりません。問題は非常に単純だと思いますが、私は Ruby on Rails にかなり慣れていないため、困惑しています...
Devise 認証用のすべてのログイン資格情報を保持する User モデルを作成しました。すべてのユーザー設定 (名など) を保持する別の Profile モデルがあります。最後に、プロファイルに関連付けられたポリモーフィック アソシエーションを使用する Address モデルがあります。
ユーザー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
理想的には、各工場を互いに独立してテストできるようにしたいと考えています。私の User モデル テストでは、次のような有効なファクトリが必要です。
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 やウェブ全体を見てきましたが、検索に適切な用語を使用していないのではないかと心配しています。また、私が見つけた各検索結果では、FactoryGirl ですべてを混合構文で行う 4 つの異なる方法があるようです。
洞察を事前にありがとう...
更新: 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
すべてのテストに合格!