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 '[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
これは私にとって本当の頭難問です。おそらくこれは私がよりよく探索し提出する必要があるバグです。私の最初の推測では、工場やテストのセットアップで単純なものが欠けているだけです。このコードは開発時にうまく機能します。どんな助けでも大歓迎です。
私が使用した場合のエラーは同じです:bill.votes << Vote.new(:value => value、:user_id => self.id、:group_id => g.id) – bonhoffer