Factory Girl + Mongoid osadzone dokumenty w oprawach
Załóżmy, że masz następujące dokumenty mongoid:
class User
include Mongoid::Document
embeds_one :name
end
class UserName
include Mongoid::Document
field :first
field :last_initial
embedded_in :user
end
Jak utworzyć fabrykę factory girl, która inicjalizuje osadzone imię i ostatni inicjał? Jak byś to zrobił w związku embeds_many
?
35
Author: Sébastien Le Callonnec, 2011-09-24
2 answers
Szukałem również tego i podczas badania natknąłem się na dużo kodu i poskładałem je wszystkie razem (szkoda, że nie ma lepszych dokumentów), ale oto moja część kodu. Adres to 1..1 związek i telefony to 1..N związek z wydarzeniami.
factory :event do
title 'Example Event'
address { FactoryGirl.build(:address) }
phones { [FactoryGirl.build(:phone1), FactoryGirl.build(:phone2)] }
end
factory :address do
place 'foobar tower'
street 'foobar st.'
city 'foobar city'
end
factory :phone1, :class => :phone do
code '432'
number '1234567890'
end
factory :phone2, :class => :phone do
code '432'
number '0987654321'
end
(i przepraszam, jeśli nie mogę podać moich linków, były trochę popaprane)
62
Author: index,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-04 17:15:53
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-04 17:15:53
Oto rozwiązanie, które pozwala dynamicznie definiować liczbę osadzonych obiektów:
FactoryGirl.define do
factory :profile do
name 'John Doe'
email '[email protected]'
user
factory :profile_with_notes do
ignore do
notes_count 2
end
after(:build) do |profile, evaluator|
evaluator.notes_count.times do
profile.notes.build(FactoryGirl.attributes_for(:note))
end
end
end
end
end
Pozwala to na wywołanie FactoryGirl.create(:profile_with_notes)
i uzyskanie dwóch osadzonych nut lub wywołanie FactoryGirl.create(:profile_with_notes, notes_count: 5)
i uzyskanie pięciu osadzonych nut. 6
Author: emkman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-21 21:37:27
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-21 21:37:27