File: auto_inject_spec.rb

package info (click to toggle)
ruby-dependor 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 264 kB
  • sloc: ruby: 557; makefile: 2; sh: 1
file content (114 lines) | stat: -rw-r--r-- 2,933 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require '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

  class DependsOnFactoryMethod
    takes :create_foo
  end

  module SomeModule
    class SampleClassWithinSomeModule
    end
  end

  class SomeFactory
    attr_reader :foo, :sample_class_with_no_dependencies

    def initialize(foo, sample_class_with_no_dependencies)
      @foo = foo
      @sample_class_with_no_dependencies = sample_class_with_no_dependencies
    end
  end

  class SampleInjector
    include Dependor::AutoInject
    look_in_modules SomeModule

    def manual_dep
      "manual dep"
    end

    def create_foo(foo_name)
      inject(SomeFactory, foo: foo_name)
    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

  context 'autoinjecting with custom dependencies' do
    it "injects the dependencies which are not explicitly specified" do
      injector.create_foo("the foo name").sample_class_with_no_dependencies.should be_an_instance_of(SampleClassWithNoDependencies)
    end

    it "passes through the dependencies that were specified" do
      injector.create_foo("the foo name").foo.should == "the foo name"
    end
  end

  context 'injecting factory methods' do
    it "injects factory methods as procs" do
      object = injector.depends_on_factory_method

      object.create_foo.call("test").foo.should == "test"
    end
  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