Sha256: 5c4b6bfd054f0c255090821bb6b107ccc3e7d91c8157af6999716413529293b1

Contents?: true

Size: 1.9 KB

Versions: 19

Compression:

Stored size: 1.9 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), "/../../../spec_helper.rb"))

describe Mongoid::Commands::Save do

  describe "#execute" do

    before do
      @parent_collection = stub(:save)
      @doc_collection = stub(:save)
      @parent = stub(:collection => @parent_collection,
                     :valid? => true,
                     :run_callbacks => true,
                     :parent => nil,
                     :attributes => {})
      @document = stub(:collection => @doc_collection,
                       :run_callbacks => true,
                       :parent => @parent,
                       :attributes => {})
    end

    context "when document is valid" do

      before do
        @document.expects(:valid?).returns(true)
      end

      it "runs the before and after callbacks" do
        @document.expects(:run_callbacks).with(:before_save)
        @document.expects(:run_callbacks).with(:after_save)
        Mongoid::Commands::Save.execute(@document)
      end

      it "returns the document" do
        Mongoid::Commands::Save.execute(@document).should == @document
      end

      context "when the document has a parent" do

        it "executes a save on the parent" do
          @parent_collection.expects(:save).with(@parent.attributes)
          Mongoid::Commands::Save.execute(@document)
        end

      end

      context "when the document has no parent" do

        before do
          @document.expects(:parent).returns(nil)
        end

        it "calls save on the document collection" do
          @doc_collection.expects(:save).with(@document.attributes)
          Mongoid::Commands::Save.execute(@document)
        end

      end

    end

    context "when document is invalid" do

      before do
        @document.expects(:valid?).returns(false)
      end

      it "returns false" do
        Mongoid::Commands::Save.execute(@document).should be_false
      end

    end

  end

end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
mongoid-0.8.2 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.8.1 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.7.10 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.7.9 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.7.8 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.7.7 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.7.6 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.7.5 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.7.4 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.7.3 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.7.2 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.7.1 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.7.0 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.6.10 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.6.9 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.6.8 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.6.7 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.6.6 spec/unit/mongoid/commands/save_spec.rb
mongoid-0.6.5 spec/unit/mongoid/commands/save_spec.rb