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 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 _(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)
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 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 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 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).then_return( "some_value") as spy: result = some_instance.some_method("anything", ["can"], go="here") expect(result).to_be("some_value")
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 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 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): 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)
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"))
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)
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 _(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)
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 _(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")
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 _(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)
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__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" )
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")
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() expect_assertion_error( lambda: expect(some_instance.some_method).was_not_called())
def _(self): some_instance = SomeClass() with spy_on(some_instance.some_method): expect(some_instance.some_method).was_not_called()
def test__to_be_a__can_pass(): expect(1).to_be_a(int) expect('').to_be_a(str) expect(SomeClass()).to_be_a(SomeClass)