Ejemplo n.º 1
0
def test_mock_is_specced():
    with Context():
        some_component_mock = mock(SomeComponent)
        assert isinstance(some_component_mock, SomeComponent)
        with pytest.raises(AttributeError):
            some_component_mock.bad_method()
        with pytest.raises(TypeError):
            some_component_mock()
        some_callable_component = mock(SomeCallableComponent)
        some_callable_component.return_value = 'mocked value'
        assert some_callable_component() == 'mocked value'
Ejemplo n.º 2
0
 def test_mock_is_specced(self):
     with Environment():
         some_component_mock = mock(SomeComponent)
         self.assertIsInstance(some_component_mock, SomeComponent)
         with self.assertRaises(AttributeError):
             some_component_mock.bad_method()
         with self.assertRaises(TypeError):
             some_component_mock()
         some_callable_component = mock(SomeCallableComponent)
         some_callable_component.return_value = 'mocked value'
         self.assertEqual(some_callable_component(), 'mocked value')
Ejemplo n.º 3
0
 def test_mock_always_replaces_component(self):
     with Environment():
         some_component_mock = mock(SomeComponent)
         some_component_mock.method.return_value = 'some other value'
         d = Depenedent()
         self.assertIs(d.some_component, some_component_mock)
         self.assertEqual(d.some_component.method(), 'some other value')
Ejemplo n.º 4
0
def test_mock_always_replaces_component():
    with Context():
        some_component_mock = mock(SomeComponent)
        some_component_mock.method.return_value = 'some other value'
        d = Dependent()
        assert d.some_component is some_component_mock
        assert d.some_component.method() == 'some other value'
Ejemplo n.º 5
0
    def test_mocks_are_reset_after_context_exit(self):
        with Environment():
            some_component_mock = mock(SomeComponent)
            d = Depenedent()
            self.assertIs(some_component_mock, d.some_component)

        with Environment():
            d = Depenedent()
            self.assertIsNot(some_component_mock, d.some_component)
            self.assertIsInstance(d.some_component, SomeComponent)
Ejemplo n.º 6
0
def test_mocks_are_reset_after_context_exit():
    with Context():
        some_component_mock = mock(SomeComponent)
        d = Dependent()
        assert some_component_mock is d.some_component

    with Context():
        d = Dependent()
        assert some_component_mock is not d.some_component
        assert isinstance(d.some_component, SomeComponent)
Ejemplo n.º 7
0
    def test_mock_replaces_named_value(self):
        class Dependency:
            def method(self):
                pass

        with Environment(key=Dependency()):
            mock_dependency = mock('key')
            mock_dependency.method.return_value = 'value'
            with self.assertRaises(AttributeError):
                mock_dependency.no_such_method()
            injected = inject('key')
            self.assertEqual(injected, mock_dependency)
            self.assertEqual(mock_dependency.method(), 'value')
Ejemplo n.º 8
0
def test_mock_replaces_named_value():
    class Dependency:
        def method(self):
            return 'not value'

    e = Context(key=Dependency())
    with e:
        mock_dependency = mock('key')
        mock_dependency.method.return_value = 'value'
        with pytest.raises(AttributeError):
            mock_dependency.no_such_method()
        injected = NamedDependent().key
        assert injected == mock_dependency
        assert mock_dependency.method() == 'value'
    with e:
        injected = NamedDependent().key
        assert injected.method() == 'not value'
Ejemplo n.º 9
0
 def test_cant_register_mocks_outside_environment(self):
     with self.assertRaises(NoEnvironment):
         mock(SomeComponent)