问题描述
在我的测试期间,Moingoid似乎并没有永久设置嵌入关系.在我的用户模型中,我有:
Moingoid doesn't seem to be setting embedded relationships persistently during my tests. In my user model I have:
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
其他有用的代码可能是:
Other helpful code might be:
## 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 '[email protected]'
u.name 'user'
u.roles_mask 1
u.password "secret"
u.password_confirmation "secret"
u.groups {[Factory.build(:group, {:name => 'foreign', :type => :custom})]}
end
对我来说,这是一个真正的难题.也许这是我需要更好地探索和提交的错误.我的第一个猜测是,我只是在工厂或测试设置中缺少一些简单的东西.此代码在开发中效果很好.任何帮助,我们将不胜感激.
This is a real head-scratcher for me. Perhaps this is a bug I need to better explore and submit. My first guess is that I am just missing something simple in my factories or test setup. This code works well on development. Any help is greatly appreciated.
推荐答案
我认为大多数工厂级宝石和Mongoid都有问题.我似乎记得在尝试同时使用Factory Girl和Machinist时遇到很多问题.
I think there are issues with most factory gems and Mongoid. I seem to recall having a lot of issues trying to use both Factory Girl and Machinist.
制造似乎是当前推荐的在Mongoid测试中进行对象生成的标准.对于我们所有的Mongoid项目,它都非常适合我们.
Fabrication seems to be the current recommended standard for doing object generation in tests with Mongoid. It's worked perfectly for us on all our Mongoid projects.
这篇关于蒙古人与工厂打得不好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!