コード例 #1
0
def test_create_async_spy(decoy: Decoy, call_handler: CallHandler) -> None:
    """It should get default configurations from the spec."""
    spec = decoy.mock(cls=Spec)

    decoy.when(spec.get_is_async()).then_return(True)

    subject = SpyCreator(call_handler=call_handler)
    result = subject.create(spec=spec)

    assert isinstance(result, AsyncSpy)
コード例 #2
0
def test_child_spy_caching(
    decoy: Decoy,
    call_handler: CallHandler,
    spy_creator: SpyCreator,
) -> None:
    """It should create a child spy only once."""
    parent_spec = decoy.mock(cls=Spec)
    child_spec = decoy.mock(cls=Spec)
    child_spy = decoy.mock(cls=Spy)
    wrong_spy = decoy.mock(cls=Spy)

    decoy.when(parent_spec.get_child_spec("child")).then_return(child_spec)
    decoy.when(spy_creator.create(spec=child_spec,
                                  is_async=False)).then_return(
                                      child_spy,
                                      wrong_spy,
                                  )

    subject = Spy(
        spec=parent_spec,
        call_handler=call_handler,
        spy_creator=spy_creator,
    )

    assert subject.child is child_spy
    assert subject.child is child_spy
コード例 #3
0
def test_create_spy(decoy: Decoy, call_handler: CallHandler) -> None:
    """It should get default configurations from the spec."""
    spec = decoy.mock(cls=Spec)
    sig = inspect.signature(some_func)

    decoy.when(spec.get_full_name()).then_return("hello.world")
    decoy.when(spec.get_signature()).then_return(sig)
    decoy.when(spec.get_class_type()).then_return(SomeClass)

    subject = SpyCreator(call_handler=call_handler)
    result = subject.create(spec=spec, name="foo")

    assert isinstance(result, Spy)
    assert isinstance(result, SomeClass)
    assert inspect.signature(result) == sig
    assert repr(result) == "<Decoy mock `hello.world`>"
コード例 #4
0
async def test_spy_async_context_manager(
    decoy: Decoy,
    call_handler: CallHandler,
    spy_creator: SpyCreator,
    spec: Spec,
) -> None:
    """It should be usable in an `async with` statement."""
    enter_spec = decoy.mock(cls=Spec)
    exit_spec = decoy.mock(cls=Spec)
    enter_spy = decoy.mock(cls=AsyncSpy)
    exit_spy = decoy.mock(cls=AsyncSpy)
    error = RuntimeError("oh no")

    decoy.when(spec.get_name()).then_return("spy_name")
    decoy.when(spec.get_child_spec("__aenter__")).then_return(enter_spec)
    decoy.when(spec.get_child_spec("__aexit__")).then_return(exit_spec)
    decoy.when(spy_creator.create(spec=enter_spec,
                                  is_async=True)).then_return(enter_spy)
    decoy.when(spy_creator.create(spec=exit_spec,
                                  is_async=True)).then_return(exit_spy)
    decoy.when(await enter_spy()).then_return(42)

    subject = Spy(spec=spec,
                  call_handler=call_handler,
                  spy_creator=spy_creator)
    tb = None

    try:
        async with subject as result:
            assert result == 42
            raise error
    except RuntimeError:
        tb = sys.exc_info()[2]
        pass

    decoy.verify(await exit_spy(RuntimeError, error, tb))
コード例 #5
0
def test_mock_with_name(
    decoy: Decoy,
    spy_creator: SpyCreator,
    call_handler: CallHandler,
    subject: DecoyCore,
) -> None:
    """It should create a generic spy by default."""
    spy = decoy.mock(cls=Spy)

    decoy.when(spy_creator.create(spec=None, name="my-spy",
                                  is_async=False)).then_return(spy)

    result = subject.mock(name="my-spy")

    assert result is spy
コード例 #6
0
def test_child_spy(
    decoy: Decoy,
    call_handler: CallHandler,
    spy_creator: SpyCreator,
) -> None:
    """It should create a child spy."""
    parent_spec = decoy.mock(cls=Spec)
    child_spec = decoy.mock(cls=Spec)
    child_spy = decoy.mock(cls=Spy)

    decoy.when(parent_spec.get_child_spec("child")).then_return(child_spec)
    decoy.when(spy_creator.create(spec=child_spec,
                                  is_async=False)).then_return(child_spy)

    subject = Spy(
        spec=parent_spec,
        call_handler=call_handler,
        spy_creator=spy_creator,
    )

    result = subject.child
    assert result is child_spy