Ejemplo n.º 1
0
def test_reset(decoy: Decoy) -> None:
    """It should be able to reset its state."""
    subject = decoy.mock(cls=SomeClass)

    subject.foo("hello")
    decoy.reset()

    with pytest.raises(AssertionError):
        decoy.verify(subject.foo("hello"))
Ejemplo n.º 2
0
def decoy() -> Iterable[Decoy]:
    """Get a [decoy.Decoy][] container and [reset it][decoy.Decoy.reset] after the test.

    This function is function-scoped [pytest fixture][] that will be
    automatically inserted by the plugin.

    [pytest fixture]: https://docs.pytest.org/en/latest/how-to/fixtures.html

    Example:
        ```python
        def test_my_thing(decoy: Decoy) -> None:
            my_fake_func = decoy.mock()
            # ...
        ```
    """
    decoy = Decoy()
    yield decoy
    decoy.reset()
Ejemplo n.º 3
0
class DecoyTestCase(unittest.TestCase):
    """Decoy test case using unittest."""
    def setUp(self) -> None:
        """Set up before each test."""
        self.decoy = Decoy()

    def tearDown(self) -> None:
        """Clean up after each test."""
        self.decoy.reset()

    def test_when(self) -> None:
        """Test that self.decoy.when works."""
        mock = self.decoy.mock(cls=SomeClass)
        self.decoy.when(mock.foo("hello")).then_return("world")
        assert mock.foo("hello") == "world"

    def test_verify(self) -> None:
        """Test that self.decoy.verify works."""
        mock = self.decoy.mock(cls=SomeClass)
        mock.foo("hello")
        self.decoy.verify(mock.foo("hello"))