在我的测试中,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 facts" ... 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
这对我来说真是令人头疼。也许这是我需要更好地探索和提交的错误。我的第一个猜测是我在工厂或测试设置中遗漏了一些简单的东西。此代码适用于开发。非常感谢任何帮助。
答案 0 :(得分:5)
我认为大多数工厂宝石和Mongoid存在问题。我似乎记得有很多问题试图同时使用Factory Girl和Machinist。
Fabrication似乎是目前推荐的使用Mongoid进行对象生成的标准。它对我们所有的Mongoid项目都很有效。