示例#1
0
文件: spy_test.py 项目: Avvir/pyne
 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)
示例#2
0
文件: spy_test.py 项目: Avvir/pyne
 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"
         }))
示例#3
0
文件: spy_test.py 项目: Avvir/pyne
 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)
示例#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)
示例#5
0
文件: spy_test.py 项目: Avvir/pyne
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()
示例#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()
示例#7
0
文件: spy_test.py 项目: Avvir/pyne
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"}))
示例#8
0
文件: spy_test.py 项目: Avvir/pyne
 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")
示例#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)
示例#10
0
文件: spy_test.py 项目: Avvir/pyne
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")
示例#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()
示例#12
0
文件: spy_test.py 项目: Avvir/pyne
        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)
示例#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"))
示例#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)
示例#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"])
示例#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)
示例#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
示例#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"]))
示例#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\(.*>")
示例#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\(.*>")
示例#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")
示例#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')>"""
    )
示例#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)
示例#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
示例#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"
    )
示例#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")
示例#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"}))
示例#28
0
 def _(self):
     some_instance = SomeClass()
     expect_assertion_error(
         lambda: expect(some_instance.some_method).was_not_called())
示例#29
0
 def _(self):
     some_instance = SomeClass()
     with spy_on(some_instance.some_method):
         expect(some_instance.some_method).was_not_called()
示例#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)