Sha256: f8882698ab52242442e9d93a52120025947cf481e561a55197af417452783436

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require_relative '../spec_helper'

require 'dependor/shorty'

describe Dependor::AutoInject do
  class SampleClassWithNoDependencies
  end

  class SampleClassWithDependency
    takes :sample_class_with_no_dependencies
  end

  class SampleClassWithManualDependency
    takes :manual_dep
  end

  module SomeModule
    class SampleClassWithinSomeModule
    end
  end

  class SampleInjector
    include Dependor::AutoInject
    look_in_modules SomeModule

    def manual_dep
      "manual dep"
    end
  end

  let(:injector) { SampleInjector.new }

  shared_examples_for 'dependency injector' do
    it 'responds to the object name' do
      injector.should respond_to(object_name)
    end

    it 'creates the object' do
      injector.send(object_name).should be_an_instance_of(object_class)
    end
  end

  context 'no dependencies' do
    let(:object_name) { :sample_class_with_no_dependencies }
    let(:object_class) { SampleClassWithNoDependencies }

    it_behaves_like 'dependency injector'
  end

  context 'dependencies on other objects' do
    let(:object_name) { :sample_class_with_dependency }
    let(:object_class) { SampleClassWithDependency }

    it_behaves_like 'dependency injector'
  end

  context 'dependencies on objects returned by methods on the injector' do
    let(:object_name) { :sample_class_with_manual_dependency }
    let(:object_class) { SampleClassWithManualDependency }

    it_behaves_like 'dependency injector'
  end

  context 'dependencies from another module' do
    let(:object_name) { :sample_class_within_some_module }
    let(:object_class) { SomeModule::SampleClassWithinSomeModule }

    it_behaves_like 'dependency injector'
  end

  it "raises an error if the object is not found" do
    proc{ injector.unknown_object }.should raise_exception(Dependor::UnknownObject)
  end

  it 'responds to regular methods on injector' do
    injector.should respond_to(:manual_dep)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dependor-0.0.1 spec/dependor/auto_inject_spec.rb