Sha256: e5004e530bdd0268d381877c8913109343defa0aa589243c8ec76c685347a1f0

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

require 'spec_helper'

RSpec.describe ResourceKit::MethodFactory do
  let(:collection) { ResourceKit::ResourceCollection.new }
  let(:klass) { Class.new { attr_accessor :connection } }

  describe '.construct' do
    before do
      collection.action :find
      collection.action :all
    end

    it 'adds the methods from the resource collection to the passed object' do
      ResourceKit::MethodFactory.construct(klass, collection)
      instance = klass.new
      expect(instance).to respond_to(:find, :all)
    end

    it 'bails when the method is already defined' do
      collection.action :all

      expect {
        ResourceKit::MethodFactory.construct(klass, collection)
      }.to raise_exception(ArgumentError).with_message("Action 'all' is already defined on `#{klass}`")
    end

    it 'adds the correct interface for the action' do
      ResourceKit::MethodFactory.construct(klass, collection)
      method_sig = klass.instance_method(:all).parameters

      expect(method_sig).to eq([[:rest, :args]])
    end

  end

  describe 'Calling the method' do
    it 'calls the invoker passed with the arguments and action' do
      action = ResourceKit::Action.new(:bunk)
      collection << action
      invoker = double('invoker', call: true)

      ResourceKit::MethodFactory.construct(klass, collection, invoker)

      instance = klass.new
      instance.connection = double('connection')
      instance.bunk('something', 'something else')
      expect(invoker).to have_received(:call).with(action, instance.connection, 'something', 'something else')
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
resource_kit-0.0.5 spec/lib/resource_kit/method_factory_spec.rb
resource_kit-0.0.3 spec/lib/resource_kit/method_factory_spec.rb
resource_kit-0.0.1 spec/lib/resource_kit/method_factory_spec.rb