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): with AttachedSpy(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(AttachedSpy.get_spy( SomeClass.some_class_method)).to_be(spy)
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 AttachedSpy(some_instance, "some_decorated_method") as spy: some_instance.some_decorated_method("anything", ["can"], go="here") expect(spy.last_call).to_be((("anything", ["can"]), { "go": "here" })) expect( AttachedSpy.get_spy( some_instance.some_decorated_method)).to_be(spy)
def get_spy(object): if isinstance(object, Spy): return object return AttachedSpy.get_spy(object)