Sha256: d8c2ee0f78887f2d892c38b827300c645fb42ba5e688c8e02977fb7097f2ec1b

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

require 'test_helper'

describe ApiResponder::Formattable do
  let(:resource)  { Object.new }
  let(:cls)       { Class.new.tap { |c| c.send :include, ApiResponder::Formattable }}
  let(:decorator) { cls.new }

  describe "#api_formats" do
    it "should have active json as default" do
      assert_equal [ :json ], cls.api_formats
    end

    it "should return list of active formats" do
      cls.api_formats :xml
      assert_equal [ :json, :xml ], cls.api_formats
    end

    it "should have as_json method that delegates to as_api" do
      decorator.expects(:as_api).with({:format => :json}).returns(resource)
      resource.expects(:as_json).with({}).returns({})
      decorator.as_json({})
    end

    it "should create to_format method that delegates to as_api" do
      cls.api_formats :xml
      decorator.expects(:as_api).with({:format => :xml}).returns(resource)
      resource.expects(:to_xml).with({}).returns("")

      decorator.to_xml({})
    end
  end

  describe "#to_format" do
    it "should merge options" do
      cls.api_formats :xml
      decorator.expects(:as_api).with({:format => :xml, :include_root => true}).returns(resource)
      resource.expects(:to_xml).with({:include_root => true}).returns("")

      decorator.to_xml({:include_root => true})
    end
  end

  describe "#as_api" do
    it "should delegate to to_api_v{api_version}" do
      decorator.expects(:as_api_v2).with({:format => :json}).returns({})

      decorator.as_api({:format => :json, :api_version => 2})
    end

    it "should raise exception if no version is given" do
      assert_raises ApiResponder::Formattable::UnsupportedVersion do
        decorator.as_api({:format => :json})
      end
    end

    it "should raise exception if no version helper exists" do
      assert_raises ApiResponder::Formattable::UnsupportedVersion do
        decorator.as_api({:format => :json, :api_version => 4})
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
api-responder-1.0.1 test/formattable_test.rb
api-responder-1.0.0 test/formattable_test.rb