好的,我所遇到的问题是FactoryGirl在我的测验中构建嵌入式作业,它使用mongo而不是活动记录。我尝试使用一个与我的活动记录模型一起使用的build_list,但我在使用mongoid时遇到了这个问题......
我可以调用以下内容并返回quiz_assignments:
@quiznos.quiz_assignments.new(due_at: Time.now+ 1.day, published_at: Time.now)
但是如果我打电话
@quiznos = FactoryGirl.build(:quizWassignments)
@quiznos将有一个创建的测验,但是@ quiz.quiz_assignments == []
我甚至可以运行以下内容并过去
@quiz = FactoryGirl.build(:quiz)
@quiznos = FactoryGirl.build(:quiz_assignment, quiz: @quiz)
@quiz.quiz_assignments.should == [@quiznos]
有没有办法让它与之合作:quizWassignments?
class Quiz
include Mongoid::Document
include Mongoid::Timestamps
field :user_id
field :title
field :description
field :assignment_id
field :due_at, :type => DateTime
field :published_at, :type => DateTime
embeds_many :quiz_assignments
end
class QuizAssignment
include Mongoid::Document
include Mongoid::Timestamps
field :due_at, :type => DateTime
field :published_at, :type => DateTime
embedded_in :quiz
embeds_many :quiz_assignees
validates_presence_of :due_at, :published_at
validates_associated :quiz_assignees
end
FactoryGirl.define do
factory :quiz do
title { Factory.next(:name) }
description { Factory.next(:description) }
quiz_type "Practice"
factory :quizWassignments do
ignore do
count 3
end
after_create do |quiz, evaluator|
FactoryGirl.build_list(:quiz_assignment, evaluator.count, quiz: quiz)
end
end
end
factory :quiz_assignment do
due_at Time.now + 1.day
published_at Time.now
end
end
答案 0 :(得分:6)
当我第一次从Active Record切换到Mongoid时,我在让Factory Girl玩得很开心方面遇到了很多麻烦。我最终切换到Fabrication - 支持Mongoid开箱即用。