Sha256: 4dbe3acf50ac04c0b16aec8bb56274e1ae78dcb7e7d5dfbd09c63d848ba6d59b

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'ostruct'

class DependencyInjectionExampleTest < Minitest::Test
  def test_dependency_injection
    obj = Invokr.inject(
      TestObject,
      :using => {
        :album => 'farmhouse',
        :guitarist => 'trey',
        :drummer => 'fishman',
      },
    )

    assert_equal 'farmhouse', obj.album
    assert_equal 'trey', obj.guitarist
  end

  def test_injecting_a_proc
    my_proc = lambda { |foo,bar| OpenStruct.new foo: foo, bar: bar }

    obj = Invokr.inject(
      my_proc,
      :using => {
        :foo => 'bar',
        :bar => 'baz',
        :ping => 'pong',
      }
    )

    assert_equal 'bar', obj.foo
    assert_equal 'baz', obj.bar
  end

  def test_injecting_proc_duck_type
    obj = Invokr.inject(
      TestProcDuckType.new,
      :using => {
        :foo => 'FOO',
        :bar => 'BAZ',
      },
    )

    assert_equal 'FOO', obj.foo
    assert_equal 'BAZ', obj.bar
  end

  class TestObject
    attr :album, :guitarist

    def initialize album, guitarist = 'jimmy'
      @album = album
      @guitarist = guitarist
    end
  end

  class TestProcDuckType
    def parameters
      [[:req, :foo],[:req, :bar]]
    end

    def call(foo, bar)
      OpenStruct.new foo: foo, bar: bar
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
invokr-0.9.5 test/dependency_injection_test.rb