def test_constructor_binding__should_construct_singleton(self):
        injector = Injector(
            lambda binder: binder.bind_to_constructor(int, random))
        instance0 = injector.get_instance(int)
        instance1 = injector.get_instance(int)

        assert instance0 == instance1
    def test_runtime_binding__should_create_runtime_singleton(self):
        class MyClass(object):
            pass

        injector = Injector()
        instance0 = injector.get_instance(MyClass)
        instance1 = injector.get_instance(MyClass)

        assert instance0 is instance1
        assert isinstance(instance0, MyClass)
Example #3
0
    def test_runtime_binding__should_create_runtime_singleton(self):
        class MyClass(object):
            pass

        injector = Injector()
        instance0 = injector.get_instance(MyClass)
        instance1 = injector.get_instance(MyClass)

        assert instance0 is instance1
        assert isinstance(instance0, MyClass)
class LazyImportTestCase(unittest.TestCase):
    
    def setUp(self):
        self.injector = Injector()
        self.injector.register()
    
    def tearDown(self):
        self.injector.unregister()
    
    def testObjProperty(self):
        '''LazyImport should lazily reference an object.'''
        self.assertTrue(LazyRef.obj is Ref)
    
    def testHashEq(self):
        '''LazyImport should be equal to the ref object, and has the same hash.'''
        self.assertEqual(hash(LazyRef), hash(Ref))
        self.assertEqual(LazyRef, Ref)
    
    def testInjection(self):
        '''LazyImport in injections.'''
        class B(object):
            a = inject.attr(inject.lazy('inject_tests.fixtures.lazy.A'))
        b = B()
        
        from inject_tests.fixtures.lazy import A
        a = A()
        self.injector.bind(A, a)
        
        self.assertTrue(b.a is a) 
 def test_instance_binding__should_use_the_same_instance(self):
     injector = Injector(lambda binder: binder.bind(int, 123))
     instance = injector.get_instance(int)
     assert instance == 123
 def test_runtime_binding__not_callable(self):
     injector = Injector()
     self.assertRaisesRegexp(
         InjectorException,
         'Cannot create a runtime binding, the key is not callable, key=123',
         injector.get_instance, 123)
 def test_provider_binding__should_call_provider_for_each_injection(self):
     injector = Injector(
         lambda binder: binder.bind_to_provider(int, random))
     instance0 = injector.get_instance(int)
     instance1 = injector.get_instance(int)
     assert instance0 != instance1
Example #8
0
 def test_provider_binding__should_call_provider_for_each_injection(self):
     injector = Injector(lambda binder: binder.bind_to_provider(int, random))
     instance0 = injector.get_instance(int)
     instance1 = injector.get_instance(int)
     assert instance0 != instance1
Example #9
0
    def test_constructor_binding__should_construct_singleton(self):
        injector = Injector(lambda binder: binder.bind_to_constructor(int, random))
        instance0 = injector.get_instance(int)
        instance1 = injector.get_instance(int)

        assert instance0 == instance1
Example #10
0
 def test_instance_binding__should_use_the_same_instance(self):
     injector = Injector(lambda binder: binder.bind(int, 123))
     instance = injector.get_instance(int)
     assert instance == 123
Example #11
0
 def test_runtime_binding__disabled(self):
     injector = Injector(bind_in_runtime=False)
     self.assertRaisesRegex(InjectorException,
                            "No binding was found for key=<.* 'int'>",
                            injector.get_instance, int)
Example #12
0
 def setUp(self):
     self.injector = Injector()
     self.injector.register()