Beispiel #1
0
def test_function_arguments_described_when_not_all_expectations_are_satisfied():
    @funk.with_mocks
    def function(mocks):
        mock = mocks.mock()
        expects_call(mock).with_args("positional", key="word")
        
    assert_raises_str(AssertionError,
                      "Not all expectations were satisfied. Expected call: unnamed('positional', key='word')",
                      function)
Beispiel #2
0
def test_function_raises_exception_if_expectations_of_calls_on_mock_are_not_satisfied():
    @funk.with_mocks
    def function(mocks):
        mock = mocks.mock()
        expects_call(mock)
        
    assert_raises_str(AssertionError,
                      "Not all expectations were satisfied. Expected call: unnamed",
                      function)
Beispiel #3
0
def test_argument_mismatches_are_show_on_separate_lines(mocks):
    mock = mocks.mock(name="database")
    allows(mock).save("Apples", "Bananas")
    
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: database.save('Apples', 'Peaches')
The following expectations on database.save did not match:
    database.save('Apples' [matched],
                  'Bananas' [got 'Peaches'])""",
                      lambda: mock.save("Apples", "Peaches"))
Beispiel #4
0
def test_calling_allowed_call_in_wrong_place_raises_assertion_error(mocks):
    file = mocks.mock()
    ordering = mocks.sequence()
    allows(file).write.in_sequence(ordering)
    expects(file).close().in_sequence(ordering)
    
    file.close()
    assert_raises_str(AssertionError,
                      'Invocation out of order. Expected no more calls in sequence, but got unnamed.write.',
                      lambda: file.write("Bar"))
Beispiel #5
0
def test_if_name_is_not_provided_type_is_converted_to_name_if_supplied(mocks):
    class UserRepository(object):
        def fetch_all(self):
            pass
    mock = mocks.mock(UserRepository)
    allows(mock).fetch_all()
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: user_repository.fetch_all(2)
The following expectations on user_repository.fetch_all did not match:
    user_repository.fetch_all() [wrong number of positional arguments]""",
                      lambda: mock.fetch_all(2))
Beispiel #6
0
def test_if_mock_is_based_on_a_class_then_can_only_expect_methods_defined_on_that_class(mocks):
    class Database(object):
        def save(self):
            pass
            
        status = False
    
    database = mocks.mock(Database)
    allows(database).save
    assert_raises_str(AssertionError, "Method 'delete' is not defined on type object 'Database'", lambda: allows(database).delete)
    assert_raises_str(AssertionError, "Attribute 'status' is not callable on type object 'Database'", lambda: allows(database).status)
Beispiel #7
0
def test_no_expectations_listed_if_none_set(mocks):
    class UserRepository(object):
        def fetch_all(self):
            pass
    mock = mocks.mock(UserRepository)
    
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: user_repository.fetch_all()
The following expectations on user_repository.fetch_all did not match:
    No expectations set.""",
                      lambda: mock.fetch_all())
Beispiel #8
0
def test_assertion_fails_if_calls_do_not_follow_sequence():
    @funk.with_mocks
    def test_function(mocks):
        file = mocks.mock(name="file")
        ordering = mocks.sequence()
        expects(file).write("Private investigations").in_sequence(ordering)
        expects(file).close().in_sequence(ordering)
        
        file.close()
        file.write("Private investigations")
        
    assert_raises_str(AssertionError,
                      "Invocation out of order. Expected file.write('Private investigations'), but got file.close().",
                      test_function)
Beispiel #9
0
def test_unexpected_invocations_display_method_name_and_parameters(mocks):
    class Database(object):
        def save(self):
            pass
            
    mock = mocks.mock(Database)
    allows(mock).save.with_args()
    
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: database.save('positional')
The following expectations on database.save did not match:
    database.save() [wrong number of positional arguments]""",
                      lambda: mock.save("positional"))
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: database.save(key='word')
The following expectations on database.save did not match:
    database.save() [unexpected keyword arguments: key]""",
                      lambda: mock.save(key="word"))
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: database.save('one', 'two', foo='bar', key='word')
The following expectations on database.save did not match:
    database.save() [wrong number of positional arguments]""",
                      lambda: mock.save("one", "two", key="word", foo="bar"))
    
    mock = mocks.mock(Database)
    allows(mock).save.with_args(1)
    
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: database.save()
The following expectations on database.save did not match:
    database.save(1) [wrong number of positional arguments]""",
                      lambda: mock.save())
Beispiel #10
0
def test_assert_that_raises_assertion_error_describing_expected_and_actual_results():
    class HasZeroLength(Matcher):
        def matches(self, value, failure_out):
            if len(value):
                failure_out.append("got <value of length %s>" % len(value))
                return False
            
            return True
            
        def __str__(self):
            return "<value of length zero>"
            
    assert_that([], HasZeroLength())
    assert_raises_str(AssertionError, 
                      "Expected: <value of length zero>\nbut: got <value of length 8>",
                      lambda: assert_that("Anything", HasZeroLength()))
Beispiel #11
0
def test_name_of_mock_is_used_in_exceptions_and_expectations_on_that_method_are_shown(mocks):
    unnamed = mocks.mock()
    named = mocks.mock(name='database')
    allows(unnamed).save.with_args("positional")
    allows(named).save.with_args("positional")
    
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: unnamed.save()
The following expectations on unnamed.save did not match:
    unnamed.save('positional') [wrong number of positional arguments]""",
                      lambda: unnamed.save())
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: database.save()
The following expectations on database.save did not match:
    database.save('positional') [wrong number of positional arguments]""",
                      lambda: named.save())
Beispiel #12
0
def test_sequence_raises_assertion_error_if_actual_call_out_of_order():
    class StubbedCall(object):
        def __init__(self, name):
            self._name = name

        def __str__(self):
            return self._name

        def is_satisfied(self):
            return False

    first_call = StubbedCall("hand(in=hand)")
    second_call = StubbedCall("why(worry)")

    sequence = Sequence()
    sequence.add_expected_call(first_call)
    sequence.add_expected_call(second_call)
    assert_raises_str(
        AssertionError,
        "Invocation out of order. Expected hand(in=hand), but got why(worry).",
        lambda: sequence.add_actual_call(second_call),
    )
Beispiel #13
0
def test_assertion_error_is_raised_when_we_have_an_expected_call():
    class StubbedCall(object):
        def __init__(self, name):
            self._name = name

        def __str__(self):
            return self._name

        def is_satisfied(self):
            return True

    first_call = StubbedCall("hand(in=hand)")
    second_call = StubbedCall("why(worry)")

    sequence = Sequence()
    sequence.add_expected_call(first_call)
    sequence.add_expected_call(second_call)

    sequence.add_actual_call(second_call)
    assert_raises_str(
        AssertionError,
        "Invocation out of order. Expected no more calls in sequence, but got hand(in=hand).",
        lambda: sequence.add_actual_call(first_call),
    )
Beispiel #14
0
def test_unexpected_calls_on_mocks_display_mock_name_and_parameters(mocks):
    return_value = "Hello!"
    mock = mocks.mock(name='save')
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: save()
The following expectations on save did not match:
    No expectations set.""",
                      mock)
    expects_call(mock).returns(return_value)
    mock()
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: save()
The following expectations on save did not match:
    save [expectation has already been satisfied]""",
                      mock)
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: save('positional', key='word')
The following expectations on save did not match:
    save [expectation has already been satisfied]""",
                      lambda: mock("positional", key="word"))
Beispiel #15
0
def test_error_is_raised_if_called_too_many_times():
    call = Call('save', IntegerCallCount(2))
    call()
    call()
    assert_raises_str(FunkyError, "Cannot call any more times", lambda: call())
Beispiel #16
0
def test_assert_raises_str_fails_if_wrong_exception_raised():
    def func():
        raise TypeError("Oh noes!")
    assert_raises(TypeError, lambda: assert_raises_str(KeyError, "Oh noes!", func))
Beispiel #17
0
def test_assert_raises_str_fails_if_no_exception_raised():
    assert_raises(AssertionError, lambda: assert_raises_str(TypeError, "Oh noes!", lambda: None))
Beispiel #18
0
def test_assert_raises_str_fails_if_messages_do_not_match():
    def func():
        raise TypeError("Oh dear.")
    assert_raises(AssertionError, lambda: assert_raises_str(TypeError, "Oh noes!", func))
Beispiel #19
0
def test_assert_raises_str_can_take_arguments_for_function_under_test():
    def func(name, number):
        if name == "Sir Galahad" and number == 42:
            raise RuntimeError("Look out!")
            
    assert_raises(RuntimeError, lambda: assert_raises_str(TypeError, "Oh noes!", func, "Sir Galahad", number=42))
Beispiel #20
0
def test_assert_raises_str_passes_if_test_raises_specified_exception_with_correct_message():
    def func():
        raise AssertionError("Oh noes!")
    assert_raises_str(AssertionError, "Oh noes!", func)
Beispiel #21
0
def test_error_is_raised_if_called_with_wrong_arguments():
    call = Call('save')
    call.with_args("positional")
    call("positional")
    assert_raises_str(FunkyError, "Called with wrong arguments", lambda: call(["wrong"], {}))