Exemplo n.º 1
0
 def _(self):
     some_instance = SomeClass()
     with stub('some_method', on=some_instance) as spy:
         some_instance.some_method("some_arg 123")
         expect(spy.last_call).to_be((("some_arg 123", ), {}))
         expect(Spy.get_spy(
             some_instance.some_method)).to_be(spy)
Exemplo n.º 2
0
 def _(self):
     some_instance = SomeClass()
     with stub(some_instance.some_method) as spy:
         some_instance.some_method("anything", ["can"], go="here")
         expect(spy.last_call).to_be((("anything", ["can"]), {
             "go": "here"
         }))
Exemplo n.º 3
0
 def _(self):
     with stub(SomeClass.some_class_method) as spy:
         SomeClass.some_class_method("anything", ["can"], go="here")
         expect(spy.last_call).to_be((("anything", ["can"]), {
             "go": "here"
         }))
         expect(Spy.get_spy(SomeClass.some_class_method)).to_be(spy)
Exemplo n.º 4
0
 def _(self):
     some_instance = SomeClass()
     with AttachedSpy(some_instance, "some_method") as spy:
         some_instance.some_method("some_arg")
         expect(spy.last_call).to_be((("some_arg", ), {}))
         expect(AttachedSpy.get_spy(
             some_instance.some_method)).to_be(spy)
Exemplo n.º 5
0
def test__restore__after_stubbing_an_instance_method__sets_the_method_back_to_what_it_originally_was(
):
    instance = SomeClass()
    when(instance.some_method).then_return("some value")

    instance.some_method.restore()
    expect(instance.some_method("anything", ["can"], go="here")).to_be_none()
Exemplo n.º 6
0
def test__for_an_instance_method__was_called__can_pass():
    some_instance = SomeClass()

    spy_on(some_instance.some_method)

    some_instance.some_method()
    expect(some_instance.some_method).was_called()
Exemplo n.º 7
0
def test__stub__when_passed_in_an_instance_method__tracks_what_the_method_was_called_with(
):
    instance = SomeClass()
    spy = when(instance.some_method)

    instance.some_method("anything", ["can"], go="here")
    expect(spy.last_call).to_be((("anything", ["can"]), {"go": "here"}))
Exemplo n.º 8
0
 def _(self):
     some_instance = SomeClass()
     with stub(some_instance.some_method).then_return(
             "some_value") as spy:
         result = some_instance.some_method("anything", ["can"],
                                            go="here")
         expect(result).to_be("some_value")
Exemplo n.º 9
0
 def _(self):
     with AttachedSpy(SomeClass, "some_method",
                      needs_binding=True) as spy:
         some_instance = SomeClass()
         some_instance.some_method("some_arg")
         expect(spy.last_call).to_be((("some_arg", ), {}))
         expect(AttachedSpy.get_spy(
             some_instance.some_method)).to_be(spy)
Exemplo n.º 10
0
def test__then_return__returns_the_given_value_when_a_stubbed_method_is_called(
):
    instance = SomeClass()

    when(instance.some_method).then_return("some value")

    expect(instance.some_method("anything", ["can"],
                                go="here")).to_be("some value")
Exemplo n.º 11
0
 def _(self):
     some_instance = SomeClass()
     with AttachedSpy(some_instance, "some_method") as spy:
         some_instance.some_method("anything", ["can"], go="here")
         expect(spy.last_call).to_be((("anything", ["can"]), {
             "go": "here"
         }))
     expect(spy.last_call).to_be_none()
Exemplo n.º 12
0
        def _(self):
            some_instance = SomeClass()

            def some_function(*args, **kwargs):
                return "some_value"

            some_instance.some_function = some_function
            expect(lambda: stub(some_instance.some_function)
                   ).to_raise_error_of_type(ValueError)
Exemplo n.º 13
0
def test__was_called_with_matcher__when_a_positional_arg_was_passed_in_keyword_style__can_pass_with_positional_style_expectation(
):
    some_instance = SomeClass()

    spy_on(some_instance.some_other_method)

    some_instance.some_other_method(some_first_arg="some-value")
    expect(some_instance.some_other_method).to_be(
        was_called_with("some-value"))
Exemplo n.º 14
0
 def _(self):
     some_instance = SomeClass()
     with AttachedSpy(some_instance,
                      "some_method").then_return("some_value") as spy:
         result = some_instance.some_method("anything", ["can"],
                                            go="here")
         expect(result).to_be("some_value")
     result = some_instance.some_method("anything", ["can"], go="here")
     expect(result).to_be(None)
Exemplo n.º 15
0
def test__for_an_instance_method__was_called_with__can_pass():
    some_instance = SomeClass()

    spy_on(some_instance.some_method)

    some_instance.some_method("some-positional-argument",
                              ["some-array-content"])
    expect(some_instance.some_method).was_called_with(
        "some-positional-argument", ["some-array-content"])
Exemplo n.º 16
0
 def _(self):
     with AttachedSpy(SomeClass, "some_static_method") as spy:
         SomeClass.some_static_method("anything", ["can"],
                                      go="here")
         expect(spy.last_call).to_be((("anything", ["can"]), {
             "go": "here"
         }))
         expect(AttachedSpy.get_spy(
             SomeClass.some_static_method)).to_be(spy)
Exemplo n.º 17
0
def test__sandbox_reset():
    sandbox = Sandbox()
    some_class_instance = SomeClass()
    sandbox.spy(some_class_instance.some_method)

    some_class_instance.some_method("some_arg")
    assert some_class_instance.some_method.last_call == (("some_arg",), {})
    sandbox.reset()
    assert some_class_instance.some_method.last_call is None
Exemplo n.º 18
0
def test__was_called_with_matcher__supports_matchers_for_positional_arguments(
):
    some_instance = SomeClass()

    spy_on(some_instance.some_method)

    some_instance.some_method("some-positional-argument",
                              ["some-array-content"])
    expect(some_instance.some_method).to_be(
        was_called_with(anything(), ["some-array-content"]))
Exemplo n.º 19
0
def test__was_called_with_matcher__when_the_method_was_called_with_the_different_arguments__fails_with_a_message(
):
    some_instance = SomeClass()

    spy_on(some_instance.some_method)

    some_instance.some_method("some-positional-argument", "some-array-content")
    expect_expectation_to_fail_with_message(
        lambda: expect(some_instance.some_method).to_be(
            was_called_with("some-positional-argument", ["some-array-content"])
        ), "Expected <.*> to be <was_called_with\(.*>")
Exemplo n.º 20
0
def test__was_called_with_matcher__when_the_method_was_called_with_different_keyword_args__fails_with_a_message(
):
    some_instance = SomeClass()

    spy_on(some_instance.some_method)

    some_instance.some_method(some_keyword_arg="some-value")

    expect_expectation_to_fail_with_message(
        lambda: expect(some_instance.some_method).to_be(
            was_called_with(some_keyword_arg="some-other-value")),
        "Expected <.*> to be <was_called_with\(.*>")
Exemplo n.º 21
0
        def _(self):
            some_instance = SomeClass()

            def some_function(*args, **kwargs):
                return "some_value"

            some_instance.some_function = some_function

            with AttachedSpy(some_instance, "some_function").call_real():
                result = some_instance.some_function("anything", ["can"],
                                                     go="here")
                expect(result).to_be("some_value")
Exemplo n.º 22
0
def test__for_an_instance_method__was_called_with__when_the_method_was_called_with_the_wrong_parameters__fails_with_a_message(
):
    some_instance = SomeClass()

    spy_on(some_instance.some_method)

    some_instance.some_method("some-positional-argument", "some-array-content")
    expect_expectation_to_fail_with_message(
        lambda: expect(some_instance.some_method).was_called_with(
            "some-positional-argument", ["some-array-content"]),
        """Expected that <SomeClass#some_method> was called with <('some-positional-argument', ['some-array-content'])> but it was called with <('some-positional-argument', 'some-array-content')>"""
    )
Exemplo n.º 23
0
        def _(self):
            def some_function(*args, **kwargs):
                return "some_value"

            some_instance = SomeClass()
            some_instance.some_function = some_function

            with AttachedSpy(some_instance, "some_function") as spy:
                some_instance.some_function("anything", ["can"], go="here")
                expect(spy.last_call).to_be((("anything", ["can"]), {
                    "go": "here"
                }))
            expect(some_instance.some_function).to_be(some_function)
Exemplo n.º 24
0
def test__sandbox_restore():
    sandbox = Sandbox()
    some_class_instance = SomeClass()
    some_other_instace = SomeClass()
    sandbox.spy(some_class_instance.some_method)
    sandbox.spy(some_other_instace.some_positional_args_method)

    some_class_instance.some_method()
    assert sandbox._spies[0].method_name == 'some_method'
    assert sandbox._spies[1].method_name == 'some_positional_args_method'

    sandbox.restore()
    assert len(sandbox._spies) == 0
Exemplo n.º 25
0
def test__for_an_instance_method__was_called__when_there_were_no_calls__fails_with_a_message(
):
    some_instance = SomeClass()

    spy_on(some_instance.some_method)

    expect_expectation_to_fail_with_message(
        lambda: expect(some_instance.some_method).was_called(),
        "Expected that <SomeClass#some_method> was called but it was never called"
    )
Exemplo n.º 26
0
 def _(self):
     with AttachedSpy(SomeClass,
                      "some_args_method_that_returns_some_value",
                      needs_binding=True).call_real() as spy:
         some_class = SomeClass()
         expect(
             some_class.some_args_method_that_returns_some_value(
                 "some_arg",
                 some_keyword_arg="some_kwarg")).to_be("some_value")
         expect(some_class.some_args_method_that_returns_some_value
                ).was_called_with("some_arg",
                                  some_keyword_arg="some_kwarg")
Exemplo n.º 27
0
 def _(self):
     some_instance = SomeClass()
     with AttachedSpy(some_instance, "some_method") as spy:
         some_instance.some_method("anything", ["can"], go="here")
         some_instance.some_method("or", ["here"], xor="here")
         expect(spy.calls).to_have_length(2)
         expect(spy.calls[1]).to_be((("or", ["here"]), {"xor": "here"}))
Exemplo n.º 28
0
 def _(self):
     some_instance = SomeClass()
     expect_assertion_error(
         lambda: expect(some_instance.some_method).was_not_called())
Exemplo n.º 29
0
 def _(self):
     some_instance = SomeClass()
     with spy_on(some_instance.some_method):
         expect(some_instance.some_method).was_not_called()
Exemplo n.º 30
0
def test__to_be_a__can_pass():
    expect(1).to_be_a(int)
    expect('').to_be_a(str)
    expect(SomeClass()).to_be_a(SomeClass)