def test__to_raise_error_with_message__can_fail_because_no_error_is_raised(): def successful_method(): pass expect_expectation_to_fail_with_message( lambda: expect(successful_method).to_raise_error_with_message("some message"), "but no exception was raised")
def test__to_raise_error_with_message__when_actual_message_contains_curly_braces__shows_message(): def error_method(): raise Exception("{oh man} {stuff!} {whoa}") expect_expectation_to_fail_with_message( lambda: expect(error_method).to_raise_error_with_message(match("other message")), "{oh man\} {stuff!\} {whoa\}")
def test__to_raise_error_with_message__with_unmatched_matcher__failures_shows_matcher_name(): def error_method(): raise Exception("some message") expect_expectation_to_fail_with_message( lambda: expect(error_method).to_raise_error_with_message(match("other message")), ".*to raise an exception with message <match\('other message'\).*")
def test__to_raise_error_with_message__can_fail_because_the_message_is_wrong(): def error_method(): raise Exception("some message") expect_expectation_to_fail_with_message( lambda: expect(error_method).to_raise_error_with_message("some other message"), "to raise an exception with message.* but the exception was")
def test__to_raise_error_with_message__when_message_is_not_first_argument__can_fail_because_the_message_is_wrong(): def error_method(): raise subprocess.CalledProcessError(1, ['some-command', 'some-argument']) expect_expectation_to_fail_with_message( lambda: expect(error_method).to_raise_error_with_message("some other message"), "to raise an exception with message <'some other message'> but the exception was <Command '\['some-command', 'some-argument'\]' returned non-zero exit status 1.>")
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""" )
def test__for_a_static_method__was_called__when_there_were_no_calls__fails_with_a_message( ): with TemporaryClass() as SomeTemporaryClass: spy_on(SomeTemporaryClass.some_static_method, on=SomeTemporaryClass) expect_expectation_to_fail_with_message( lambda: expect(SomeTemporaryClass.some_static_method).was_called(), "Expected that <SomeTemporaryClass::some_static_method> was called but it was never called" )
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')>""" )
def test__to_raise_error_of_type__can_fail_because_no_error_is_raised(): def successful_method(): pass class SomeException(Exception): pass expect_expectation_to_fail_with_message( lambda: expect(successful_method).to_raise_error_of_type(SomeException), "but no exception was raised")
def test__to_raise_error_of_type__can_fail_because_the_type_is_wrong(): def error_method(): raise Exception("some error") class SomeException(Exception): pass expect_expectation_to_fail_with_message( lambda: expect(error_method).to_raise_error_of_type(SomeException), "to raise an exception of type <SomeException>.* but the exception was")
def test__was_called_with_matcher__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).to_be( was_called_with("some-positional-argument", ["some-array-content"]) ), "Expected <.*> to be <was_called_with\(.*>")
def test__was_called__when_the_subject_is_not_a_spy__fails_with_message(): def some_non_spy(): pass some_non_spy() expect_expectation_to_fail_with_message( lambda: expect(some_non_spy).was_called(), """Expected that <tests.test_doubles.test_doubles_expectations_test.some_non_spy> was called but its calls were not tracked. Hint: use stub() to track its calls""" )
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 test__assert_expected__when_the_matcher_takes_multiple_args__prints_the_failure_reason( ): expectation = Expectation( "to_meet_some_conditions_of", Matcher("some_matcher", lambda subject, *params: False)) expect_expectation_to_fail_with_message( lambda: expectation.assert_expected("some-subject", "first-arg", 222222, "third-arg"), "Expected <'some-subject'> to meet some conditions of <<'first-arg'>, <222222>, <'third-arg'>>" )
def test__assert_expected__when_the_matcher_takes_no_args__prints_the_failure_reason( ): class MatcherWithNoArgs(Matcher): def __init__(self): super().__init__("some_matcher", lambda subject, *params: False) expectation = Expectation("to_meet_some_condition", MatcherWithNoArgs()) expect_expectation_to_fail_with_message( lambda: expectation.assert_expected("some-subject"), "Expected <'some-subject'> to meet some condition")
def test__for_a_static_method__was_called_with__when_there_were_no_calls__fails_with_a_message( ): with TemporaryClass() as SomeTemporaryClass: spy_on(SomeTemporaryClass.some_static_method, on=SomeTemporaryClass) expect_expectation_to_fail_with_message( lambda: expect( SomeTemporaryClass.some_static_method).was_called_with( "some-positional-argument", ["some-array-content"]), """Expected that <SomeTemporaryClass::some_static_method> was called with <('some-positional-argument', ['some-array-content'])> but it was never called""" )
def test__for_an_instance_method__was_called_with__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_with( "some-positional-argument", ["some-array-content"]), """Expected that <SomeClass#some_method> was called with <('some-positional-argument', ['some-array-content'])> but it was never called""" )
def test__assert_expected__when_the_matcher_comparator_fails__treats_it_as_not_matching( ): def comparator_that_raises(subject, *params): return subject.do_something() is True expectation = Expectation("to_meet_some_condition", Matcher("some_matcher", comparator_that_raises)) expect_expectation_to_fail_with_message( lambda: expectation.assert_expected("some-subject"), "Expected <'some-subject'> to meet some condition but comparator raised <AttributeError: 'str' object has no attribute 'do_something'>" )
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 test__assert_expected__when_the_matcher_match_fails__treats_it_as_not_matching( ): class BadMatcher(Matcher): def matches(self, subject): raise Exception("some error when matching") expectation = Expectation("to_meet_some_condition", BadMatcher('bad_matcher', equal_to_comparator)) expect_expectation_to_fail_with_message( lambda: expectation.assert_expected("some-subject"), "Expected <'some-subject'> to meet some condition but matcher raised <Exception: some error when matching>" )
def test__assert_expected__when_the_matcher_has_a_failure_reason__prints_the_failure_reason( ): class MatcherWithFailureReason(Matcher): def __init__(self): super().__init__("some_matcher", lambda subject, *params: False) def reason(self): return "it was not like that, yo" expectation = Expectation("to_meet_some_condition", MatcherWithFailureReason()) expect_expectation_to_fail_with_message( lambda: expectation.assert_expected("some-subject"), "Expected <'some-subject'> to meet some condition" " but it was not like that, yo")
def test__to_contain__when_item_not_contained__fails_with_message(): expect_expectation_to_fail_with_message( lambda: expect(["some-item"]).to_contain("some-other-item"), "Expected <\['some-item'\]> to contain <'some-other-item'>")
def test__to_be_between__when_item_is_less__fails_with_message(): expect_expectation_to_fail_with_message( lambda: expect(1).to_be_between(2, 3), "Expected <1> to be between <2> and <3> but it was less than or equal to <2>")
def test__to_have_length__fails_with_message(): expect_expectation_to_fail_with_message( lambda: expect([123, 123, 123, "123"]).to_have_length(5), "Expected <\[123, 123, 123, '123'\]> to have length <5> but has length <4>")
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")
def test__not_to_have_length__fails_with_message(): expect_expectation_to_fail_with_message( lambda: expect([123, 123]).not_to_have_length(2), "Expected <\[123, 123\]> not to have length <2>")
def test__to_be_about__when_the_number_is_outside_of_the_given_tolerance__fails_with_message(): expect_expectation_to_fail_with_message( lambda: expect(3.5).to_be_about(3), "Expected <3.5> to be about <3>" )
def test__to_match_list__can_fail(): expect_expectation_to_fail_with_message( lambda: expect([1, 2, 3]).to_match_list([2, 3, 4]), "Expected <[1, 2, 3]> to match list <[2, 3, 4]>" )
def test__to_be_between__when_item_is_greater__fails_with_message(): expect_expectation_to_fail_with_message( lambda: expect(3).to_be_between(2, 3), "Expected <3> to be between <2> and <3> but it was greater than or equal to <3>")
def test__not_to_be__when_equal__fails_with_message(): expect_expectation_to_fail_with_message( lambda: expect(1).not_to_be(1), "Expected <1> not to be <1>")