Sha256: b9dc65894af8eb078351e870fc0b5d73bca343b60ede61d3c2cd6844ecdd2e7d

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mm_to_view_model-0.1.1 spec/mm_to_view_model_spec.rb
mm_to_view_model-0.1.0 spec/mm_to_view_model_spec.rb