RailsアプリケーションでMongoidを使用しています。以下の構造を持つ「Post」という名前のクラスに以下のフィールドがあると考えてください
class UserPost
include Mongoid::Document
field :post, type: String
field :user_id, type: Moped::BSON::ObjectId
embeds_many :comment, :class_name => "Comment"
validates_presence_of :post, :user_id
end
-
class Comment
include Mongoid::Document
field :commented_user_id, type: Moped::BSON::ObjectId
field :comment, type: String
embedded_in :user_post, :class_name => "UserPost"
end
このモデルは、値を挿入するときに完璧に機能します。
しかし今、私はこのモデルのテストの作成に取り組んでおり、Factory girl を使用してテスト データをロードしています。で「UserPost」モデルの
モデルフィールドをプロットする方法に混乱しています/spec/factories/user_posts.rb
。
以下の形式で試しましたが、機能しません(たとえば、一部のフィールドのみが追加されます)
FactoryGirl.define do
factory :user_post do
id Moped::BSON::ObjectId("50ffd609253ff1bfb2000002")
post "Good day..!!"
user_id Moped::BSON::ObjectId("50ffd609253ff1bfb2000002")
comment :comment
end
factory :comment do
id Moped::BSON::ObjectId("50ffd609253ff1bfb2000002")
end
end