def test_was_never_called_is_false(self): self.spy.one_arg_method(1) try: assert_that_method(self.spy.one_arg_method).was_never_called() self.fail("it was called indeed!") except UnexpectedBehavior: pass
def test_matcher_error_is_human_readable(self): self.spy.one_arg_method('xe') try: assert_that_method(self.spy.one_arg_method).was_called().with_args( starts_with('tt')) except ArgsDontMatch as e: self.assertTrue("tt" in str(e.args[0])) self.assertTrue("string starting" in str(e.args[0]))
def test_obj_with_field_defends_agains_wrong_usage(self): self.spy.one_arg_method(Collaborator()) try: assert_that_method(self.spy.one_arg_method).was_called().with_args( obj_with_fields('id = 20')) self.fail('Wrong assertion, argument should be a dictionary') except WrongApiUsage: pass
def test_expect_several_times(self): self.spy.one_arg_method(1) try: assert_that_method(self.spy.one_arg_method).was_called().times(2) self.fail("Should have been called 2 times") except UnexpectedBehavior: pass
def test_args_match_but_not_number_of_times(self): self.spy.one_arg_method(1) self.spy.one_arg_method(2) try: assert_that_method( self.spy.one_arg_method).was_called().with_args(1).times(2) self.fail("Wrong assertion") except UnexpectedBehavior: pass
def test_expect_several_times_with_incorrect_args(self): self.spy.one_arg_method(1) self.spy.one_arg_method(1) try: assert_that_method( self.spy.one_arg_method).was_called().with_args(2).times(2) self.fail("Must have 1 as an argument") except ArgsDontMatch: pass
def test_fail_incorrect_times_msg_is_human_readable(self): self.spy.one_arg_method(1) try: assert_that_method(self.spy.one_arg_method).was_called().times(5) self.fail("Should have been called 2 times") except UnexpectedBehavior as e: for arg in e.args: if re.search("5", str(arg)) and re.search( "one_arg_method", str(arg)): return self.fail("No enough readable exception message")
def test_obj_with_several_fields_matcher(self): obj = Collaborator() obj.id = 21 self.spy.one_arg_method(obj) try: assert_that_method(self.spy.one_arg_method).was_called().with_args( obj_with_fields({ 'id': 20, 'test_field': 'OK' })) self.fail('Wrong assertion, id field is different') except ArgsDontMatch: pass
def test_compare_objects_is_not_possible_without_eq_operator(self): class SomeObject(): field1 = field2 = None obj = SomeObject() obj2 = SomeObject() self.spy.one_arg_method(obj) try: assert_that_method( self.spy.one_arg_method).was_called().with_args(obj2) self.fail('they should not match') except ArgsDontMatch: pass
def test_other_way_of_assert_called(self): self.spy.hello() assert_that_method(self.spy.hello).was_called()
def test_obj_with_field_matcher(self): obj = Collaborator() obj.id = 20 self.spy.one_arg_method(obj) assert_that_method(self.spy.one_arg_method).was_called().with_args( obj_with_fields({'id': 20}))
def test_expect_several_times_with_args_definition(self): self.spy.one_arg_method(1) self.spy.one_arg_method(1) assert_that_method( self.spy.one_arg_method).was_called().with_args(1).times(2)
def test_expect_several_times_matches_exactly(self): self.spy.one_arg_method(1) self.spy.one_arg_method(1) assert_that_method(self.spy.one_arg_method).was_called().times(2)
def test_was_never_called(self): assert_that_method(self.spy.one_arg_method).was_never_called()