Sha256: 21c76f60f11a8427dbaddc70b1e0915a00fcaeddfab02c118931011d67c6c021

Contents?: true

Size: 1.44 KB

Versions: 6

Compression:

Stored size: 1.44 KB

Contents

require "spec_helper"

describe MongoDoc::Contexts do

  class ContextTest
    include MongoDoc::Document

    embed_many :children
  end

  context ".context_for" do
    let(:criteria) { stub('criteria', :klass => klass) }

    context "when criteria is for a top-level MongoDoc::Document" do
      let(:klass) { ContextTest }

      it "creates a Mongo context" do
        MongoDoc::Contexts::Mongo.should_receive(:new).with(criteria)
        Mongoid::Contexts.context_for(criteria)
      end
    end

    context "when criteria is for an association" do
      let(:klass) { ContextTest.new.children }

      it "creates an Enumerable context" do
        Mongoid::Contexts::Enumerable.should_receive(:new).with(criteria)
        Mongoid::Contexts.context_for(criteria)
      end
    end

    context "when criteria is for a MongoDoc::Collection" do
      let(:klass) { MongoDoc::Collection.new('collection') }

      before do
        MongoDoc::Collection.stub(:mongo_collection).and_return(stub('collection'))
      end

      it "creates a Mongo context" do
        MongoDoc::Contexts::Mongo.should_receive(:new).with(criteria)
        Mongoid::Contexts.context_for(criteria)
      end
    end

    context "when criteria is not recognized" do
      let(:klass) { Object }

      it "raises an exception" do
        expect do
          Mongoid::Contexts.context_for(criteria)
        end.should raise_error(Mongoid::Contexts::UnknownContext)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
mongo_doc-0.6.1 spec/contexts_spec.rb
mongo_doc-0.6.0 spec/contexts_spec.rb
mongo_doc_rails2-0.6.1 spec/contexts_spec.rb
mongo_doc-0.5.5 spec/contexts_spec.rb
mongo_doc-0.4.2 spec/contexts_spec.rb
mongo_doc-0.4.1 spec/contexts_spec.rb