Sha256: 8403c07a1d18d445d0a501cd4d8f32bc64ad46b22816838718c39542cce06d16

Contents?: true

Size: 1.46 KB

Versions: 19

Compression:

Stored size: 1.46 KB

Contents

require 'spec_helper'

describe Shutl::Resource::Rest do

  class TestResource
    include Shutl::Resource::Rest
  end

  class TestOverride
    include Shutl::Resource::Rest
    resource_name 'a_other_prefix'
  end

  let(:resource) { TestResource.new({ a: 'a', b: 'b' }) }

  describe '#initalize' do
    it 'should assign the instance variable value for the entries in the hash' do
      resource.attributes['a'].should == 'a'
      resource.attributes['b'].should == 'b'
    end

    it 'should create a attribute reader' do
      resource.a().should == 'a'
    end

    it 'should keep the method not found behaviour' do
      lambda { resource.notfound() }.should raise_error(NameError)
    end
  end

  describe '#to_json' do

    it 'should "prefix" the json with the name of the class' do
      json = resource.to_json

      JSON.parse(json, symbolize_names: true)[:test_resource].should_not be_nil
    end

    it 'should serialize in json based on the instance variables' do
      json = resource.to_json

      JSON.parse(json, symbolize_names: true).should == { test_resource: { a: 'a', b: 'b' } }
    end

    it 'should be able to overribe the prefix' do
      json = TestOverride.new({ a: 'a'}).to_json

      JSON.parse(json, symbolize_names: true)[:a_other_prefix].should_not be_empty

    end
  end

  describe 'udpate_attributes' do

    it 'should replace the attributes' do
      resource.update_attributes(a: 'c')

      resource['a'].should == 'c'
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
shutl_resource-2.0.2 spec/dynamic_resource_spec.rb
shutl_resource-2.0.1 spec/dynamic_resource_spec.rb
shutl_resource-2.0.0 spec/dynamic_resource_spec.rb
shutl_resource-1.9.1 spec/dynamic_resource_spec.rb
shutl_resource-1.9.0 spec/dynamic_resource_spec.rb
shutl_resource-1.8.2 spec/dynamic_resource_spec.rb
shutl_resource-1.8.0 spec/dynamic_resource_spec.rb
shutl_resource-1.7.3 spec/dynamic_resource_spec.rb
shutl_resource-1.7.2 spec/dynamic_resource_spec.rb
shutl_resource-1.7.1 spec/dynamic_resource_spec.rb
shutl_resource-1.7.0 spec/dynamic_resource_spec.rb
shutl_resource-1.6.0 spec/dynamic_resource_spec.rb
shutl_resource-1.5.6 spec/dynamic_resource_spec.rb
shutl_resource-1.5.5 spec/dynamic_resource_spec.rb
shutl_resource-1.5.4 spec/dynamic_resource_spec.rb
shutl_resource-1.5.3 spec/dynamic_resource_spec.rb
shutl_resource-1.5.2 spec/dynamic_resource_spec.rb
shutl_resource-1.5.1 spec/dynamic_resource_spec.rb
shutl_resource-1.5.0 spec/dynamic_resource_spec.rb