Sha256: 9248867ef946cac1b9f94c35d2db88444d89069ac8e110a0fa10a13833589f3d

Contents?: true

Size: 1.86 KB

Versions: 3

Compression:

Stored size: 1.86 KB

Contents

require "spec_helper"

describe ApiClient::Resource::Base do

  describe '.scope' do

    it "is an instance of ApiClient::Resource::Scope" do
      ApiClient::Resource::Base.scope.should be_an_instance_of(ApiClient::Resource::Scope)
    end

  end


  describe "persistence" do

    before do
      @instance = ApiClient::Resource::Base.new
      @instance.id = 42
      @instance.name = "Mike"
    end

    describe '#persisted?' do

      it "returns true if id is present, false otherwise" do
        @instance.id = 42
        @instance.persisted?.should == true
        @instance.id = nil
        @instance.persisted?.should == false
      end

    end

    describe '#save' do

      it "creates a record if not persisted" do
        @instance.id = nil
        @instance.should_receive(:remote_create)
        @instance.save
      end

      it "updates a record if not persisted" do
        @instance.id = 42
        @instance.should_receive(:remote_update)
        @instance.save
      end

    end

    describe "#destroy" do

      it "delegates the destroy to the class" do
        ApiClient::Resource::Base.should_receive(:destroy).with(42)
        @instance.destroy
      end

    end

    describe "#remote_update" do

      it "delegates the update to the class" do
        ApiClient::Resource::Base.should_receive(:update).with(42, "name" => "Mike")
        @instance.remote_update
      end

      it "retains the original scope" do
        ApiClient::Resource::Base.stub(:update)
        @instance.original_scope = stub
        @instance.original_scope.should_receive(:update).with(42, "name" => "Mike")
        @instance.remote_update
      end

    end

    describe "#remote_create" do

      it "delegates the create to the class" do
        ApiClient::Resource::Base.should_receive(:create).with("name" => "Mike")
        @instance.remote_create
      end

    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
api_client-0.3.0 spec/api_client/resource/base_spec.rb
api_client-0.2.0 spec/api_client/resource/base_spec.rb
api_client-0.1.9 spec/api_client/resource/base_spec.rb