def test__eq__with_different_injectables(self): # given injectable_a = Injectable(None) injectable_b = Injectable(None) # then assert injectable_a != injectable_b
def test__eq__with_equal_injectables(self): # given injectable_a = Injectable(MagicMock(), unique_id="0") injectable_b = Injectable(MagicMock(), unique_id="0") # then assert injectable_a.constructor != injectable_b.constructor assert injectable_a == injectable_b
def test__get_instance__with_non_singleton_injectable(self): # given constructor = MagicMock(side_effect=["call_0", "call_1"]) injectable = Injectable(constructor, singleton=False) # when instance_0 = injectable.get_instance() instance_1 = injectable.get_instance() # then assert constructor.call_count == 2 assert instance_0 != instance_1
def __init__(self): pass clear_injectables(Car) mocked_car = Mock(wraps=Car) mocked_car.forward = Mock(side_effect=lambda: print("driving but not for real")) mocked_injectable = Injectable(lambda: mocked_car) register_injectables({mocked_injectable}, Car)
def test__get_instance__with_lazy_instance(self): # given constructor = MagicMock() injectable = Injectable(constructor) # when instance = injectable.get_instance(lazy=True) # then assert constructor.called is False # and when instance.anything() # then assert constructor.called is True
def __init__(self): clear_injectables(RealCar) mocked_car = Mock(wraps=RealCar) mocked_car.print = Mock( side_effect=lambda: print("driving but not for real")) mocked_injectable = Injectable(lambda: mocked_car) register_injectables({mocked_injectable}, RealCar)
def mock_car(): clear_injectables("car") mocked_car = Mock(wraps=Car) mocked_car.forward = Mock( side_effect=lambda: print("driving but not for real")) mocked_car_injectable = Injectable(lambda: mocked_car) register_injectables({mocked_car_injectable}, qualifier="car")
def test__init__defaults(self): # given constructor = MagicMock() # when injectable = Injectable(constructor) # then assert injectable.constructor == constructor assert injectable.unique_id is not None assert injectable.primary is False assert injectable.group is None assert injectable.singleton is False
def mock_configurator(): clear_injectables(Configurator) mocked_configuration = Mock(wraps=Configurator) configuration = { 'bicycle': { 'speed_limit': '1000', }, 'car': { 'speed_limit': '900' } } mocked_configuration.get_speed_limit = Mock( side_effect=lambda type: configuration.get(type, {}).get( 'speed_limit', '')) mocked_configuration_injectable = Injectable(lambda: mocked_configuration) register_injectables({mocked_configuration_injectable}, Configurator)
def __init__(self): clear_injectables(RealDep) mocked_dep = Mock(wraps=RealDep) mocked_dep.print = Mock(side_effect=lambda: print("MockedDep")) mocked_injectable = Injectable(lambda: mocked_dep) register_injectables({mocked_injectable}, RealDep)
def foo(): mocked_injectable = Injectable(lambda: object()) register_injectables({mocked_injectable}, object) assert inject(object)