Exemple #1
0
def test__restore__resets_what_the_spy_was_last_called_with():
    spy = Spy()

    spy("anything", ["can"], go="here")
    expect(spy.last_call).to_be((("anything", ["can"]), {"go": "here"}))

    spy.restore()
    expect(spy.last_call).to_be_none()
Exemple #2
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)
    def comparator(self, subject, params):
        was_called_matcher = WasCalledMatcher()
        if not was_called_matcher.matches(subject):
            self._reason = was_called_matcher.reason()
            return False
        subject = Spy.get_spy(subject)
        expected_call_args, expected_call_kwargs = params

        call_args, call_kwargs = subject.last_call

        expected_named_args = _get_all_named_args(subject.signature,
                                                  expected_call_args,
                                                  expected_call_kwargs)
        actual_named_args = _get_all_named_args(subject.signature, call_args,
                                                call_kwargs)

        expected_varargs = _get_varargs(subject.signature, expected_call_args)
        actual_varargs = _get_varargs(subject.signature, call_args)

        matches_varargs = MatchesListMatcher(expected_varargs)
        matches_named_args = MatchesDictMatcher(expected_named_args)
        self._reason = "it was called with <" + format_arguments(
            call_args, call_kwargs) + ">"
        return matches_varargs.list_comparator(actual_varargs, expected_varargs) and \
               matches_named_args.comparator(actual_named_args, expected_named_args)
Exemple #4
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)
Exemple #5
0
def stub(method, on=None):
    if on is None and hasattr(method, '__self__'):
        object_to_stub = method.__self__
    else:
        object_to_stub = on

    spy = Spy(object_to_stub, method)
    return spy
Exemple #6
0
def test__was_called_with__when_there_were_no_calls__fails_with_a_message():
    spy = Spy()

    expect_expectation_to_fail_with_message(
        lambda: expect(spy).was_called_with("some-positional-argument",
                                            ["some-array-content"]),
        """Expected that <Spy#__call__> was called with <('some-positional-argument', ['some-array-content'])> but it was never called"""
    )
Exemple #7
0
def test__was_called_with__when_the_method_was_called_with_the_wrong_parameters__fails_with_a_message(
):
    spy = Spy()
    spy("some-positional-argument", "some-array-content")
    expect_expectation_to_fail_with_message(
        lambda: expect(spy).was_called_with("some-positional-argument",
                                            ["some-array-content"]),
        """Expected that <Spy#__call__> was called with <('some-positional-argument', ['some-array-content'])> but it was called with <('some-positional-argument', 'some-array-content')>"""
    )
Exemple #8
0
 def _(self):
     with stub(some_class.some_module_method, on=some_class) as spy:
         some_class.some_module_method("anything", ["can"],
                                       go="here")
         expect(spy.last_call).to_be((("anything", ["can"]), {
             "go": "here"
         }))
         expect(Spy.get_spy(
             some_class.some_module_method)).to_be(spy)
Exemple #9
0
 def comparator(self, subject):
     subject = Spy.get_spy(subject)
     if not hasattr(subject, 'last_call'):
         self._reason = "its calls were not tracked. Hint: use stub() to track its calls"
         return False
     elif subject.last_call is None:
         self._reason = "it was never called"
         return False
     else:
         return True
Exemple #10
0
def test__restore__causes_the_spy_to_return_none():
    spy = Spy()

    spy.then_return("some value")
    expect(spy("anything", ["can"], go="here")).to_be("some value")

    spy.restore()
    expect(spy("anything", ["can"], go="here")).to_be_none()
Exemple #11
0
 def unmatcherify(self, params, subject):
     return Expectation.unmatcherify(self, params, Spy.get_spy(subject))
Exemple #12
0
def test__was_called_with__can_pass():
    spy = Spy()

    spy("some-positional-argument", ["some-array-content"])
    expect(spy).was_called_with("some-positional-argument",
                                ["some-array-content"])
Exemple #13
0
def test__was_called__when_there_were_no_calls__fails_with_a_message():
    spy = Spy()

    expect_expectation_to_fail_with_message(
        lambda: expect(spy).was_called(),
        "Expected that <Spy#__call__> was called but it was never called")
Exemple #14
0
def test__was_called__can_pass():
    spy = Spy()

    spy()

    expect(spy).was_called()
Exemple #15
0
def test__tracks_what_it_was_called_with():
    spy = Spy()

    spy("anything", ["can"], go="here")
    assert spy.last_call == (("anything", ["can"]), {"go": "here"})
Exemple #16
0
def test__exit__calls_when_with_statement_is_exited():
    with Spy() as spy:
        spy("anything", ["can"], go="here")
        expect(spy.last_call).to_be((("anything", ["can"]), {"go": "here"}))
    expect(spy.last_call).to_be_none()
Exemple #17
0
def test__tracks_calls():
    spy = Spy()
    spy("arg1", kwarg1="kwarg1")
    spy("arg2", kwarg2="kwarg2")
    assert spy.calls[0] == (("arg1", ), {"kwarg1": "kwarg1"})
    assert spy.calls[1] == (("arg2", ), {"kwarg2": "kwarg2"})
Exemple #18
0
def test__then_return__returns_the_given_value_when_the_spy_is_called():
    spy = Spy()

    spy.then_return("some value")
    expect(spy("anything", ["can"], go="here")).to_be("some value")