Skip to content
Browse files

Remove duplication in specs to make them more readable.

  • Loading branch information...
1 parent 3ea2c48 commit f191449aee8bcd54bfb5ded91101d0f9dcb63c52 @stesla committed Aug 13, 2009
Showing with 71 additions and 82 deletions.
  1. +71 −82 spec/injector_spec.rb
View
153 spec/injector_spec.rb
@@ -4,43 +4,60 @@
include Tourniquet
describe Tourniquet::Injector do
+ before(:each) do
+ @injector = Injector.new
+ end
+
+ def implementation_for(interface, &block)
+ klass = Class.new(&block)
+ klass.class_eval "def #{interface}?; true; end"
+ klass
+ end
+
+ def with_binding(interface, &block)
+ klass = implementation_for(interface, &block)
+ @injector.bind(interface).to(klass)
+ end
+
+ def with_cached_binding(interface, &block)
+ klass = Class.new(&block)
+ @injector.bind(interface).cached.to(klass)
+ end
+
+ def an_instance_of(interface, &block)
+ instance = @injector[interface]
+ block.call(instance)
+ end
+
it 'should blow up if there is no binding for a class' do
- lambda { Injector.new[:foo] }.should raise_error(NotFound)
+ lambda { @injector[:foo] }.should raise_error(NotFound)
end
it 'should find a class that has used the inject keyword' do
- klass = Class.new { inject }
- injector = Injector.new do |i|
- i.bind(:klass).to(klass)
+ with_binding(:klass) { inject }
+ an_instance_of :klass do |obj|
+ obj.should_not be_nil
+ obj.should be_klass
end
- injector[:klass].should be_instance_of(klass)
end
it 'should pass in dependencies' do
- dep_foo = Class.new { inject }
- dep_bar = Class.new { inject }
-
- klass = Class.new do
+ with_binding(:foo) { inject }
+ with_binding(:bar) { inject }
+ with_binding(:klass) do
inject :foo => :foo, :bar => :bar
attr_reader :foo, :bar
end
- injector = Injector.new do |i|
- i.bind(:foo).to(dep_foo)
- i.bind(:bar).to(dep_bar)
- i.bind(:klass).to(klass)
+ an_instance_of :klass do |k|
+ k.should be_klass
+ k.foo.should be_foo
+ k.bar.should be_bar
end
-
- k = injector[:klass]
- k.should be_instance_of(klass)
- k.foo.should_not be_nil
- k.foo.should be_instance_of(dep_foo)
- k.bar.should_not be_nil
- k.bar.should be_instance_of(dep_bar)
end
it 'should call #after_initialize after dependencies are set' do
- klass = Class.new do
+ with_binding(:klass) do
inject
attr_reader :foo
@@ -49,13 +66,9 @@ def after_initialize
end
end
- injector = Injector.new do |i|
- i.bind(:klass).to(klass)
+ an_instance_of :klass do |k|
+ k.foo.should be_empty
end
-
- k = injector[:klass]
- k.foo.should_not be_nil
- k.foo.should be_empty
end
it 'should allow depenencies to be injected manually for testing' do
@@ -69,18 +82,13 @@ def after_initialize
end
it 'should figure out simple linear dependencies' do
- klass1 = Class.new { inject }
- klass2 = Class.new { inject :one => :klass1; attr_reader :one }
- klass3 = Class.new { inject :two => :klass2; attr_reader :two }
-
- injector = Injector.new do |i|
- i.bind(:klass1).to(klass1)
- i.bind(:klass2).to(klass2)
- i.bind(:klass3).to(klass3)
- end
+ with_binding(:klass1) { inject }
+ with_binding(:klass2) { inject :one => :klass1; attr_reader :one }
+ with_binding(:klass3) { inject :two => :klass2; attr_reader :two }
- k = injector[:klass3]
- k.two.one.should be_instance_of(klass1)
+ an_instance_of :klass3 do |k|
+ k.two.one.should be_klass1
+ end
end
it 'should requre dep keys to be symbols' do
@@ -98,17 +106,11 @@ def after_initialize
end
it 'should blow up on a circular dependency' do
- klass1 = Class.new { inject :x => :klass3 }
- klass2 = Class.new { inject :x => :klass1 }
- klass3 = Class.new { inject :x => :klass2 }
-
- injector = Injector.new do |i|
- i.bind(:klass1).to(klass1)
- i.bind(:klass2).to(klass2)
- i.bind(:klass3).to(klass3)
- end
+ with_binding(:klass1) { inject :x => :klass3 }
+ with_binding(:klass2) { inject :x => :klass1 }
+ with_binding(:klass3) { inject :x => :klass2 }
- lambda { k = injector[:klass1] }.should raise_error(CircularDependency)
+ lambda { @injector[:klass1] }.should raise_error(CircularDependency)
end
it 'should give me a new instance every time by default' do
@@ -119,66 +121,53 @@ def after_initialize
it 'should give me a cached instance if I tell it to' do
klass = Class.new { inject }
- injector = Injector.new {|i| i.bind(:klass).cached.to(klass) }
- injector[:klass].should == injector[:klass]
+ @injector.bind(:klass).cached.to(klass)
+ @injector[:klass].should == @injector[:klass]
end
describe 'with caching enabled and circular dependencies' do
before (:each) do
- klass1 = Class.new { inject :two => :klass2; attr_reader :two }
- klass2 = Class.new { inject :one => :klass1; attr_reader :one }
-
- @injector = Injector.new do |i|
- i.bind(:klass1).cached.to(klass1)
- i.bind(:klass2).to(klass2)
- end
+ with_cached_binding(:klass1) { inject :two => :klass2; attr_reader :two }
+ with_binding(:klass2) { inject :one => :klass1; attr_reader :one }
end
it 'should not blow up' do
lambda { @injector[:klass1] }.should_not raise_error
end
it 'should have circular references' do
- one = @injector[:klass1]
- one.should respond_to(:two)
- one.two.should respond_to(:one)
- one.two.one.should respond_to(:two)
- one.two.one.two.should == one.two
+ an_instance_of :klass1 do |one|
+ one.should respond_to(:two)
+ one.two.should respond_to(:one)
+ one.two.one.should respond_to(:two)
+ one.two.one.two.should == one.two
+ end
end
end
it 'cannot bind more than one implementation for an interface' do
- klass1 = Class.new { inject }
- klass2 = Class.new { inject }
-
- injector = Injector.new do |i|
- i.bind(:foo).to(klass1)
- end
- lambda { injector.bind(:foo).to(klass2) }.should raise_error(AlreadyBound)
+ with_binding(:klass) { inject }
+ another_klass = Class.new { inject }
+ lambda { @injector.bind(:klass).to(another_klass) }.should raise_error(AlreadyBound)
end
it 'can bind a specific instance' do
- injector = Injector.new do |i|
- i.bind(:db_user).to_instance("bob")
+ @injector.bind(:db_user).to_instance("bob")
+ an_instance_of :db_user do |obj|
+ obj.should == "bob"
end
-
- injector[:db_user].should == "bob"
end
it 'should bind providers' do
- klass1 = Class.new { inject }
- klass2 = Class.new do
+ with_binding(:klass1) { inject }
+ with_binding(:klass2) do
inject :provider => :klass1.provider
attr_reader :provider
end
- injector = Injector.new do |i|
- i.bind(:klass1).to(klass1)
- i.bind(:klass2).to(klass2)
+ an_instance_of :klass2 do |obj|
+ obj.provider.should respond_to(:get_instance)
+ obj.provider.get_instance.should be_klass1
end
-
- k = injector[:klass2]
- k.provider.should respond_to(:get_instance)
- k.provider.get_instance.should be_instance_of(klass1)
end
end

0 comments on commit f191449

Please sign in to comment.
Something went wrong with that request. Please try again.