Sha256: cee3165906a0c125506c8b9537b3c84543d813fe9fc9956cd927d15afcbae42d

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

require 'spec_helper'

class User
  include MongoMapper::Document
  plugin MongoMapper::Plugins::ToViewModel
  
  key :name, String
  key :hobbies, Array
  key :unemployed, Boolean, :default => false
  
  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" => [],
        "unemployed" => false,
        "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],
        "unemployed" => false
      )
    end
    
    let(:result) do
      {
        "_id" => subject.id, 
        "name" => "SomeBrodi12",
        "hobbies" => [],
        "unemployed" => false,
        "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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mm_to_view_model-0.1.2 spec/mm_to_view_model_spec.rb