0

Mongoid で Rails 3.2.11 アプリケーションを構築しています。Cucumber でテストし、FactoryGirl でテスト オブジェクトを作成します。オブジェクトを埋め込んでいます。親オブジェクトと埋め込みオブジェクトの両方で FactoryGirl トレイトを使用して、多くの順列を作成し、物事を DRY に保ちたいと考えています。

問題:埋め込みオブジェクトに適用する特性を取得できないようです。

model.rb (実際には、別のモデル ファイル)

class User    
   include Mongoid::Document

   #Fields and stuff
   embeds_one :car
end

class Car    
   include Mongoid::Document

   #Fields and stuff
   embedded_in :user 
end

スペック/factories.rb

FactoryGirl.define do
  factory :user do

    status 'active'     # shared attribute  

    trait :bob do
     name 'Bob'
     email 'bob@bob.com'
    end

    trait :carol do
     name 'Carol'
     email 'carol@carol.com'
    end

    car  { [FactoryGirl.build(:car)] }

  end

  factory :car do

    number_of_tires 4   # shared attribute

    trait :red_ford do
     make 'Ford'
     color 'Red'
    end

    trait :blue_chevy do
     make 'Chevy'
     color 'Blue'
    end

  end
end

features/step_definitions/user_steps.rb (正しく動作しない)

Given /^There is a user Bob with a Blue Chevy$/ do
  FactoryGirl.create(:user, :bob, :car => [:car => :blue_chevy])
end

埋め込みオブジェクトの特性を省略すれば、ユーザー オブジェクトを工場で作成しても問題ありません。目的の特性を適用して組み込みオブジェクトを構築する方法はありますか? ありがとう!

修正

ソートボットhttp://joshuaclayton.meの Joshua Claytonは次のように述べています。

関連に特性を適用するための簡略構文はありません。基本的には、次のようにする必要があります。

cars = [FactoryGirl.build(:car, :blue_chevy)]
FactoryGirl.create(:user, :bob, cars: cars)
4

1 に答える 1

2

1 対 1 の関係であるため、spec/factories.rb で car オブジェクトをビルドするときに配列は必要ありません。

car { FactoryGirl.build(:car) }

これはキュウリのステップにも当てはまります。また、私が理解しているように、特性は属性として使用されるため、ステップコードは次のようになります

FactoryGirl.create(:user, :bob, :car, :blue_chevy)

于 2013-03-07T08:28:49.377 に答える