示例#1
0
    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)
示例#2
0
    def test_mock__try_to_mock_method_out_of_context(self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                my_thing_mock: MyThing = tmock(mocked_thing)

                with self.assertRaises(NoBehaviourSpecifiedError):
                    when(my_thing_mock.return_a_str()).then_return(
                        "some string")
示例#3
0
    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")
示例#4
0
    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()
示例#5
0
    def test_mock__can_mock_method__default_args(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=1)).then_return(None)

                my_thing_mock.method_with_default_args(1)

                verify(my_thing_mock).method_with_default_args(1)
示例#6
0
    def test_mock__can_mock_method__no_args__no_return(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.do_something_with_side_effects()
                         ).then_return(None)

                my_thing_mock.do_something_with_side_effects()

                verify(my_thing_mock).do_something_with_side_effects()
示例#7
0
    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)
示例#8
0
    async def test_we_can_mock_and_verify_an_async_method(self):
        expected = "Hello"

        with tmock(MyAsyncThing) as my_async_mock:
            when(await
                 my_async_mock.get_an_async_result()).then_return(expected)

        self.assertEqual(expected, await my_async_mock.get_an_async_result())

        verify(my_async_mock).get_an_async_result()
示例#9
0
    def test_mock__generic_attribute__get__simple_return(self):
        for mocked_thing in mocked_things:
            with self.subTest():
                expected = ["hello"]

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.generic_att).then_return(expected)

                actual = my_thing_mock.generic_att

                self.assertEqual(expected, actual)
示例#10
0
    def test_mock__property__get__simple_return(self):
        for mocked_thing in mocked_things:
            with self.subTest():
                expected = "hello"

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.derived_property).then_return(expected)

                actual = my_thing_mock.derived_property

                self.assertEqual(expected, actual)
示例#11
0
    def test_mock__class_attribute__get__then_raise(self):
        expected_error = IOError()

        for mocked_thing in mocked_things:
            with self.subTest():
                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.class_att_with_type).then_raise(
                        expected_error)

                with self.assertRaises(IOError):
                    my_thing_mock.class_att_with_type
示例#12
0
    def test_mock__then_raise(self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                expected_error = IOError()

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.do_something_with_side_effects()
                         ).then_raise(expected_error)

                with self.assertRaises(IOError):
                    my_thing_mock.do_something_with_side_effects()
示例#13
0
    def test_mock__class_attribute__get__simple_return(self):
        for mocked_thing in mocked_things:
            with self.subTest():
                expected = 3

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.class_att_with_type).then_return(
                        expected)

                actual = my_thing_mock.class_att_with_type

                self.assertEqual(expected, actual)
示例#14
0
    def test_mock__instance_attribute__get__simple_return(self):
        expected = 2

        for mocked_thing in mocked_things:
            with self.subTest():
                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.instance_att_typed_init).then_return(
                        expected)

                actual = my_thing_mock.instance_att_typed_init

                self.assertEqual(expected, actual)
示例#15
0
    def test_try_to_specify_non_type_safe_return_type__simple_type(self):
        for type_safety in TypeSafety:
            with self.subTest():
                # Method
                with self.assertRaises(MockTypeSafetyError):
                    with tmock(MyThing, type_safety=type_safety) as my_thing_mock:
                        when(my_thing_mock.convert_int_to_str(1)).then_return(2)

                # Attribute get
                with self.assertRaises(MockTypeSafetyError):
                    with tmock(MyThing, type_safety=type_safety) as my_thing_mock:
                        when(my_thing_mock.a_hinted_str_attribute).then_return(1)
示例#16
0
    def test_mock__class_attribute__get__many(self):
        expected_responses = [3, 4]

        for mocked_thing in mocked_things:
            with self.subTest():

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.class_att_with_type).then_return_many(
                        expected_responses)

                for expected in expected_responses:
                    actual = my_thing_mock.class_att_with_type
                    self.assertEqual(expected, actual)
示例#17
0
    def test_mock__then_return_many__loop_true(self):
        expected_responses = ["first response", "second response"]

        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.return_a_str()).then_return_many(
                        expected_responses, True)

                for i in range(2):
                    for expected in expected_responses:
                        actual = my_thing_mock.return_a_str()
                        self.assertEqual(expected, actual)
示例#18
0
    def test_mock__can_mock_method__arg__returns(self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                expected_result = "a string"

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.convert_int_to_str(1)).then_return(
                        expected_result)

                actual = my_thing_mock.convert_int_to_str(1)

                self.assertEqual(expected_result, actual)
                verify(my_thing_mock).convert_int_to_str(1)
示例#19
0
    def test_mock__class_attribute__set_then_get(self):
        for mocked_thing in mocked_things:
            with self.subTest():
                mocked_with = 1

                with tmock(MyThing) as my_thing_mock:
                    when(my_thing_mock.instance_att_typed_init).then_return(
                        mocked_with)

                setting_to = 3

                my_thing_mock.class_att_with_type = setting_to

                self.assertEqual(mocked_thing.class_att_with_type, mocked_with)
示例#20
0
    def test_mock__can_mock_method__multiple_args__mixed_with_kwargs_in_setup(
            self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                expected_result = "a string"

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.multiple_arg(
                        number=1, prefix="p")).then_return(expected_result)

                actual = my_thing_mock.multiple_arg("p", 1)

                self.assertEqual(expected_result, actual)
                verify(my_thing_mock).multiple_arg("p", 1)
示例#21
0
    def test_mock__class_attribute__get__then_do(self):
        expected = 3

        def handle_get():
            return expected

        for mocked_thing in mocked_things:
            with self.subTest():
                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.class_att_with_type).then_do(handle_get)

                actual = my_thing_mock.class_att_with_type

                self.assertEqual(expected, actual)
示例#22
0
    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()
示例#23
0
    def test_mock__can_mock_method__object_arg__returns(self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                expected_result = 2

                object_arg = NestedThing()

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.method_with_object(
                        object_arg)).then_return(expected_result)

                actual = my_thing_mock.method_with_object(object_arg)

                self.assertEqual(expected_result, actual)
                verify(my_thing_mock).method_with_object(object_arg)
示例#24
0
    def test_verify__exactly__not_matched__no_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.do_something_with_side_effects()).then_return(
                None)

        # Method
        with self.assertRaises(VerifyError):
            verify(my_thing_mock, exactly=1).do_something_with_side_effects()

        # Get attribute
        with self.assertRaises(VerifyError):
            verify(my_thing_mock, exactly=1).some_instance_attribute

        # Set attribute
        with self.assertRaises(VerifyError):
            verify(my_thing_mock, exactly=1).some_instance_attribute = "bye"
示例#25
0
    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"
示例#26
0
    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)
示例#27
0
    def test_mock__then_return_many__loop_false(self):
        expected_responses = ["first response", "second response"]

        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.return_a_str()).then_return_many(
                        expected_responses, False)

                for expected in expected_responses:
                    actual = my_thing_mock.return_a_str()
                    self.assertEqual(expected, actual)

                # Not looping, and responses have run out.
                with self.assertRaises(NoBehaviourSpecifiedError):
                    my_thing_mock.return_a_str()
示例#28
0
    def test_verify__at_least__was_called(self):
        with tmock(MyThing) as my_thing_mock:
            when(my_thing_mock.some_instance_attribute).then_return("Hello")
            when(my_thing_mock.do_something_with_side_effects()).then_return(
                None)

        # Method
        my_thing_mock.do_something_with_side_effects()

        verify(my_thing_mock).do_something_with_side_effects()

        # Get Attribute
        my_thing_mock.some_instance_attribute

        verify(my_thing_mock).some_instance_attribute

        # Set Attribute
        my_thing_mock.some_instance_attribute = "bye"

        verify(my_thing_mock).some_instance_attribute = "bye"
示例#29
0
    def test_mock__can_mock_method__generic_args_and_return__returns(self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                expected_result = {"my_key": True}

                with tmock(mocked_thing) as my_thing_mock:
                    when(
                        my_thing_mock.
                        method_with_standard_generic_args_and_return(
                            list_arg=["hello"],
                            dict_arg={"foo":
                                      False})).then_return(expected_result)

                actual = my_thing_mock.method_with_standard_generic_args_and_return(
                    list_arg=["hello"], dict_arg={"foo": False})

                self.assertEqual(expected_result, actual)
                verify(my_thing_mock
                       ).method_with_standard_generic_args_and_return(
                           list_arg=["hello"], dict_arg={"foo": False})
示例#30
0
    def test_mock__can_mock_method__args_and_kwargs__returns(self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                expected_result = False

                with tmock(mocked_thing) as my_thing_mock:
                    when(
                        my_thing_mock.method_with_args_and_kwargs(
                            "a", "b", key1=1,
                            key2=2)).then_return(expected_result)

                actual = my_thing_mock.method_with_args_and_kwargs("a",
                                                                   "b",
                                                                   key1=1,
                                                                   key2=2)

                self.assertEqual(expected_result, actual)
                verify(my_thing_mock).method_with_args_and_kwargs("a",
                                                                  "b",
                                                                  key1=1,
                                                                  key2=2)