require 'spec_helper' class User include MongoMapper::Document plugin MongoMapper::Plugins::ToViewModel key :name, String key :hobbies, Array many :posts end class Post include MongoMapper::Document plugin MongoMapper::Plugins::ToViewModel key :title, String key :body, String belongs_to :user end describe User do describe ".to_view_model" do subject { User } let(:result) do { "_id" => nil, "name" => "", "hobbies" => [], "posts" => [ { "_id" => nil, "title" => "", "body" => "" } ] } end it { subject.to_view_model.should == result } end describe "#to_view_model" do let(:first_post) do Post.new("title" => "Some new cool thing", "body" => "This new cool thing rocks!") end let(:second_post) do Post.new("title" => "News about Paris", "body" => "They remade the eiffel tower out of cheese!") end subject do User.new( "name" => "SomeBrodi12", "posts" => [first_post, second_post] ) end let(:result) do { "_id" => subject.id, "name" => "SomeBrodi12", "hobbies" => [], "posts" => [ { "_id" => first_post.id, "title" => "Some new cool thing", "body" => "This new cool thing rocks!", "user_id" => subject.id }, { "_id" => second_post.id, "title" => "News about Paris", "body" => "They remade the eiffel tower out of cheese!", "user_id" => subject.id }, ] } end it { subject.to_view_model.should == result } end end