Moingoid, кажется, не устанавливает постоянные встроенные отношения во время моих тестов. В моей пользовательской модели у меня есть:
def vote_on(bill, value)
if my_groups = self.groups
my_groups.each do |g|
bill.votes.create(:value => value, :user_id => self.id, :group_id => g.id)
# result only with factories: bill.votes.first.group = nil
# and bill.votes.first.user = nil !!
# self.id and g.id have good values during the test, they just aren't persisting
end
else
raise "no groups for this user" # #{self.full_name}"
end
end
Другой полезный код может быть:
## bill model
class Bill
embeds_many :votes
## vote model
class Vote
include Mongoid::Document
field :value, :type => Symbol # can be :aye, :nay, :abstain
#field :group_type, :type => Integer
belongs_to :user
belongs_to :group
embedded_in :bill
end
## test
test "descriptive tally should work" do
user1 = Factory.build(:user)
b = Factory.build(:bill)
user1.vote_on(b, :aye) # nil values created here!
tally = b.descriptive_tally
assert_not_nil tally
end
## bill factory
Factory.define :bill do |f|
f.bill_html "just the <b>facts</b>"
...
f.state "Introduced"
f.text_updated_on DateTime.parse("2011-06-16 00:00:00 Z")
f.text_word_count 2356
f.votes
end
## user factory
Factory.define :user do |u|
u.email 'user@domain.com'
u.name 'user'
u.roles_mask 1
u.password "secret"
u.password_confirmation "secret"
u.groups {[Factory.build(:group, {:name => 'foreign', :type => :custom})]}
end
Для меня это настоящая головная боль. Возможно, это ошибка, которую мне нужно лучше изучить и отправить. Мое первое предположение состоит в том, что я просто упускаю что-то простое на своих фабриках или в тестовой установке. Этот код хорошо работает при разработке. Любая помощь с благодарностью.