我正在用Mongoid構建Rails 3.2.11應用程序。我使用Cucumber測試並使用FactoryGirl創建測試對象。我有嵌入的對象。我希望能夠將FactoryGirl特性與我的父級和嵌入式對象一起使用,以便進行大量排列並保持乾燥。將FactoryGirl特徵應用於Mongoid嵌入對象
問題:我似乎無法獲得適用於嵌入式對象的特徵。
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 '[email protected]'
end
trait :carol do
name 'Carol'
email '[email protected]'
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
功能/ 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
我可以工廠創建用戶對象就好了,如果我離開了嵌入式對象的特質。任何方式來獲得嵌入對象與所需的特質應用?謝謝!
在Thoughtbot http://joshuaclayton.me的修復
約書亞·克萊頓說:
有用於應用性狀協會沒有簡寫語法; 基本上,你不得不做這樣的事情:
cars = [FactoryGirl.build(:car, :blue_chevy)]
FactoryGirl.create(:user, :bob, cars: cars)