Sha256: 0042822b77ed2cfeb1d24b44079805c4541c4eb8ed4fb551671d38aac0931e00

Contents?: true

Size: 1.69 KB

Versions: 35

Compression:

Stored size: 1.69 KB

Contents

require 'spec_helper'

describe Defaultable do

  module SparkApi
    module Models
      class TestClass < Base
        extend Finders
        include Defaultable
        self.element_name = 'testclass'
      end
    end
  end

  describe 'default' do

    it 'returns an instance of the class' do
      allow(TestClass).to receive(:connection).and_return(double(get: [{"Name" => 'foo'}]))
      expect(TestClass.default).to be_a TestClass
    end

    it 'returns nil when there are no results' do
      allow(TestClass).to receive(:connection).and_return(double(get: []))
      expect(TestClass.default).to be nil
    end

    it "assigns the default id to the instance if it doesn't have an id" do
      allow(TestClass).to receive(:connection).and_return(double(get: [{"Name" => 'foo'}]))
      expect(TestClass.default.Id).to eq TestClass::DEFAULT_ID
    end

    it "doesn't override the id if one is present" do
      allow(TestClass).to receive(:connection).and_return(double(get: [{"Id" => '5', "Name" => 'foo'}]))
      expect(TestClass.default.Id).to eq '5'
    end
    
  end
  
  describe 'find' do

    it "calls 'default' when given the default id" do
      expect(TestClass).to receive(:default)
      TestClass.find(TestClass::DEFAULT_ID)
    end

    it "passes options to 'default'" do
      expect(TestClass).to receive(:default).with(foo: true)
      TestClass.find(TestClass::DEFAULT_ID, foo: true)
    end

    it "calls Finders.find when given a normal id" do
      connection = double
      expect(connection).to receive(:get).with("/testclass/5", {foo: true}).and_return([{}])
      allow(TestClass).to receive(:connection).and_return(connection)
      TestClass.find('5', foo: true)
    end
    
  end

end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
spark_api-1.6.0 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.5.6 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.5.5 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.5.4 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.5.3 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.5.2 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.5.1 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.5.0 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.4.34 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.4.32 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.4.31 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.4.29 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.4.28 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.4.27 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.4.26 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.4.25 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.4.24 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.4.23 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.4.22 spec/unit/spark_api/models/defaultable_spec.rb
spark_api-1.4.21 spec/unit/spark_api/models/defaultable_spec.rb