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"}))
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" }))
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()
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"}))
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)
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)
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()
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)
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") result = some_instance.some_method("anything", ["can"], go="here") expect(result).to_be(None)
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"])
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
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"]))
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\(.*>")
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\(.*>")
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')>""" )
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
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()
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")
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")
def _(self): some_instance = SomeClass() with attach_spy(some_instance, "some_method"): some_instance.some_method() expect_assertion_error(lambda: expect(some_instance.some_method).was_not_called())
def test__sandbox_when_calling_then_return(): sandbox = Sandbox() some_class_instance = SomeClass() sandbox.when_calling(some_class_instance.some_method).then_return(5) assert some_class_instance.some_method() == 5
def _(self): some_instance = SomeClass() with spy_on(some_instance.some_method): some_instance.some_method() expect_assertion_error( lambda: expect(some_instance.some_method).was_not_called())
def _(self): some_instance = SomeClass() with spy_on(some_instance.some_method): some_instance.some_method() expect(some_instance.some_method).was_called()
def _(self): with AttachedSpy(SomeClass, "some_method"): some_instance = SomeClass() some_instance.some_method("some_arg") expect(some_instance.some_method).was_called()
def _(self): some_instance = SomeClass() with attach_spy(some_instance, "some_method"): expect(some_instance.some_method).was_not_called() some_instance.some_method("anything", ["can"], go="here") expect(some_instance.some_method).was_called_with("anything", ["can"], go="here")
def _(self): some_instance = SomeClass() with attach_spy(some_instance, "some_method"): some_instance.some_method() expect(some_instance.some_method).was_called()