def test_verify__any_matcher__not_called(self): with tmock(MyThing) as my_thing_mock: when(my_thing_mock.convert_int_to_str(1)).then_return("something") when(my_thing_mock.some_instance_attribute).then_return("hello") with self.assertRaises(VerifyError): verify(my_thing_mock).convert_int_to_str(match.anything()) with self.assertRaises(VerifyError): verify(my_thing_mock).some_instance_attribute = match.anything()
def test_mock__can_mock_method__default_args_with_matchers(self): for mocked_thing in mocked_things: with self.subTest("{}".format(mocked_thing)): with tmock(mocked_thing) as my_thing_mock: when( my_thing_mock.method_with_default_args( first_number=match.anything(), second_string=match.anything())).then_return(None) my_thing_mock.method_with_default_args(1) verify(my_thing_mock).method_with_default_args(1)
def test_verify__any_matcher(self): with tmock(MyThing) as my_thing_mock: when(my_thing_mock.convert_int_to_str(1)).then_return("something") when(my_thing_mock.some_instance_attribute).then_return("hello") # Method my_thing_mock.convert_int_to_str(1) verify(my_thing_mock).convert_int_to_str(match.anything()) # Set Attribute my_thing_mock.some_instance_attribute = "bye" verify(my_thing_mock).some_instance_attribute = match.anything()
def test_specify_any_matcher_arg__called_with_correct_type__raise_error( self): with tmock(MyThing) as my_thing_mock: when(my_thing_mock.convert_int_to_str( match.anything())).then_raise(IOError) with self.assertRaises(IOError): my_thing_mock.convert_int_to_str(2)
def test_when_we_have_matcher_based_behaviour_type_safety_is_enforced_on_call( self): expected = "a string" with tmock(MyThing) as my_thing_mock: when(my_thing_mock.convert_int_to_str( match.anything())).then_return(expected) with self.assertRaises(MockTypeSafetyError): my_thing_mock.convert_int_to_str("not an int")
def test_specify_any_matcher_arg__called_with_correct_type__return_many( self): expected_many = ["a string"] with tmock(MyThing) as my_thing_mock: when(my_thing_mock.convert_int_to_str( match.anything())).then_return_many(expected_many) for expected in expected_many: actual = my_thing_mock.convert_int_to_str(2) self.assertEqual(expected, actual)
def test_mock__then_do(self): expected_arg = 1 def bounce_back_handler(number: int): assert number == expected_arg return "{}".format(number) for mocked_thing in mocked_things: with self.subTest("{}".format(mocked_thing)): with tmock(mocked_thing) as my_thing_mock: when(my_thing_mock.convert_int_to_str( match.anything())).then_do(bounce_back_handler) actual = my_thing_mock.convert_int_to_str(expected_arg) self.assertEqual("1", actual)
def test_verify__exactly__not_matched__other_calls__verify_error(self): with tmock(MyThing) as my_thing_mock: when(my_thing_mock.some_instance_attribute).then_return("Hello") when(my_thing_mock.convert_int_to_str( match.anything())).then_return("A string") my_thing_mock.convert_int_to_str(1) # Method with self.assertRaises(VerifyError): verify(my_thing_mock, exactly=2).convert_int_to_str(2) my_thing_mock.some_instance_attribute = "something else" # Set attribute with self.assertRaises(VerifyError): verify(my_thing_mock, exactly=2).some_instance_attribute = "bye"
def test_something_equal_to_any_thing(self): matcher = match.anything() self.assertEqual(1, matcher)