def And_attribute_is_a_magic_instance_method(context):

            context.memoize("call_args", lambda _: ())
            context.memoize("call_kwargs", lambda _: {})

            @context.before
            def before(self):
                target = StrictMock(template=Target)
                self.original_callable = None
                self.real_target = target
                self.target_arg = target
                self.callable_arg = "__str__"
                self.mock_callable_dsl = mock_callable(
                    self.target_arg, self.callable_arg
                )
                self.callable_target = lambda: str(target)

            context.merge_context(
                "examples for target",
                callable_accepts_no_args=True,
                has_original_callable=False,
                can_yield=False,
            )

            context.merge_context("other instances are not mocked")
示例#2
0
            def with_signature_validation_False(context):
                @context.function
                def get_signature_validation(self):
                    return False

                context.merge_context("callable attributes examples",
                                      signature_validation_value=False)
    def When_target_is_function_of_a_module(context):
        @context.before
        def before(self):
            self.original_callable = testslide._test_function
            self.real_target = testslide
            self.target_arg = "testslide"
            self.callable_arg = "_test_function"
            self.mock_callable_dsl = mock_callable(self.target_arg, self.callable_arg)
            self.callable_target = testslide._test_function

        context.merge_context("examples for target")

        @context.example
        def works_with_alternative_module_names(self):
            target = "os.path"
            target_module = os.path
            alternative_target = "testslide.cli.os.path"
            import testslide.cli

            alternative_target_module = testslide.cli.os.path
            original_function = os.path.exists

            self.mock_callable(target, "exists").for_call("found").to_return_value(True)
            self.mock_callable(alternative_target, "exists").for_call(
                "not_found"
            ).to_return_value(False)
            self.assertTrue(target_module.exists("found"))
            self.assertTrue(alternative_target_module.exists("found"))
            self.assertFalse(target_module.exists("not_found"))
            self.assertFalse(alternative_target_module.exists("not_found"))
            testslide.mock_callable.unpatch_all_callable_mocks()
            self.assertEqual(os.path.exists, original_function, "Unpatch did not work")
示例#4
0
    def common(context, fails_if_class_attribute):
        context.merge_context("patching works")

        @context.example
        def it_fails_if_attribute_is_callable(self):
            with self.assertRaisesRegex(ValueError,
                                        "^Attribute can not be callable*"):
                self.patch_attribute(self.target, self.callable_attribute,
                                     self.new_value)

        if fails_if_class_attribute:

            @context.example
            def it_fails_if_attribute_is_a_class(self):
                with self.assertRaisesRegex(ValueError,
                                            "^Attribute can not be a class*"):
                    self.patch_attribute(self.target, self.class_attribute,
                                         self.new_value)

        else:

            @context.sub_context
            def with_class_attributes(context):
                context.merge_context("patching works")

        @context.xexample
        def it_fails_if_new_value_is_of_incompatible_type(self):
            pass
示例#5
0
 def with_a_patched_runtime_attr(context):
     context.memoize(
         "target",
         lambda self: StrictMock(template=sample_module.SomeClass,
                                 runtime_attrs=["runtime_attr"]),
     )
     context.memoize("attribute", lambda self: "runtime_attr")
     context.merge_context("patching works")
        def when_target_is_an_instance(context):
            context.memoize("target", lambda self: sample_module.SomeClass())
            context.memoize("real_target", lambda self: self.target)
            context.merge_context("common", fails_if_class_attribute=False)

            @context.sub_context
            def and_attribute_is_a_property(context):
                context.merge_context("common", fails_if_class_attribute=False)
    def When_target_is_static_method_at_a_class(context):
        @context.before
        def before(self):
            self.original_callable = Target.static_method
            self.real_target = Target
            self.target_arg = Target
            self.callable_arg = "static_method"
            self.mock_callable_dsl = mock_callable(self.target_arg, self.callable_arg)
            self.callable_target = Target.static_method

        context.merge_context("examples for target")
    def When_target_is_function_of_a_module(context):
        @context.before
        def before(self):
            self.original_callable = testslide._test_function
            self.target_arg = "testslide"
            self.callable_arg = "_test_function"
            self.mock_callable_dsl = mock_callable(self.target_arg,
                                                   self.callable_arg)
            self.callable_target = testslide._test_function

        context.merge_context("examples for target")
                def fails_when(context):

                    context.merge_context("assert failure")

                    @context.example
                    def called_once(self):
                        self.callable_target(*self.call_args,
                                             **self.call_kwargs)

                    @context.example
                    def called_several_times(self):
                        for _ in range(self.times + 1):
                            self.callable_target(*self.call_args,
                                                 **self.call_kwargs)
                    def fails_when(context):

                        context.merge_context("assert failure")

                        context.merge_context("not called")
                        context.merge_context("called less times")
                        context.merge_context("called more times fail")
    def When_target_is_instance_method_at_an_instance(context):

        context.memoize("callable_arg", lambda _: "instance_method")

        @context.before
        def before(self):
            target = Target()
            self.original_callable = target.instance_method
            self.real_target = target
            self.target_arg = target
            self.mock_callable_dsl = mock_callable(self.target_arg, self.callable_arg)
            self.callable_target = target.instance_method

        context.merge_context("examples for target")
        context.merge_context("other instances are not mocked")
    def When_target_is_a_builtin(context):
        context.memoize("call_args", lambda _: (0,))
        context.memoize("call_kwargs", lambda _: {})
        context.memoize("specific_call_args", lambda _: (0.000000001,))
        context.memoize("specific_call_kwargs", lambda _: {})

        @context.before
        def before(self):
            self.original_callable = time.sleep
            self.real_target = time
            self.target_arg = "time"
            self.callable_arg = "sleep"
            self.mock_callable_dsl = mock_callable(self.target_arg, self.callable_arg)
            self.callable_target = time.sleep

        context.merge_context("examples for target", validate_signature=False)
        def And_attribute_is_a_instance_method(context):

            context.memoize("callable_arg", lambda _: "instance_method")

            @context.before
            def before(self):
                target = StrictMock(template=Target)
                self.original_callable = None
                self.target_arg = target
                self.mock_callable_dsl = mock_callable(self.target_arg,
                                                       self.callable_arg)
                self.callable_target = target.instance_method

            context.merge_context("examples for target",
                                  has_original_callable=False)

            context.merge_context("other instances are not mocked")
示例#14
0
                            def method_mocking(context):

                                context.merge_context("can access attributes")

                                @context.after
                                def after(self):
                                    self.assertEqual(
                                        getattr(
                                            self.strict_mock,
                                            self.test_method_name)("hello"),
                                        "mock: hello",
                                    )

                                @context.example
                                def can_mock_with_function(self):
                                    setattr(
                                        self.strict_mock,
                                        self.test_method_name,
                                        self.mock_function,
                                    )

                                @context.example
                                def can_mock_with_lambda(self):
                                    setattr(
                                        self.strict_mock,
                                        self.test_method_name,
                                        lambda message: "mock: {}".format(
                                            message),
                                    )

                                @context.example
                                def can_mock_with_instancemethod(self):
                                    class SomeClass(object):
                                        def mock_method(self, message):
                                            return "mock: {}".format(message)

                                    setattr(
                                        self.strict_mock,
                                        self.test_method_name,
                                        SomeClass().mock_method,
                                    )
        def magic_method_tests(context):
            @context.before
            def before(self):
                self.original_callable = self.target.__str__
                self.target_arg = self.target
                self.callable_arg = "__str__"
                self.mock_callable_dsl = mock_callable(self.target_arg,
                                                       self.callable_arg)
                self.callable_target = lambda: str(self.target)

            context.merge_context("examples for target",
                                  callable_accepts_no_args=True,
                                  can_yield=False)

            @context.example
            def other_instances_are_not_mocked(self):
                mock_callable(
                    self.target_arg,
                    self.callable_arg).to_return_value("mocked value")
                self.assertEqual(self.callable_target(), "mocked value")
                self.assertEqual(str(Target()), "original response")
    def When_target_is_static_method_at_an_instance(context):
        @context.before
        def before(self):
            target = Target()
            self.original_callable = target.static_method
            self.real_target = target
            self.target_arg = target
            self.callable_arg = "static_method"
            self.mock_callable_dsl = mock_callable(self.target_arg, self.callable_arg)
            self.callable_target = target.static_method

        context.merge_context("examples for target")
        context.merge_context("other instances are not mocked")
        context.merge_context("class is not mocked")
        def And_attribute_is_a_static_method(context):
            @context.before
            def before(self):
                target = StrictMock(template=Target)
                self.original_callable = None
                self.target_arg = target
                self.callable_arg = "static_method"
                self.mock_callable_dsl = mock_callable(self.target_arg,
                                                       self.callable_arg)
                self.callable_target = target.static_method

            context.merge_context("examples for target",
                                  has_original_callable=False)
            context.merge_context("other instances are not mocked")
            context.merge_context("class is not mocked")
                def fails_when(context):

                    context.merge_context("assert failure")

                    context.merge_context("not called")
示例#19
0
 def without_a_template(context):
     context.memoize("target", lambda self: StrictMock())
     context.merge_context("patching works")
示例#20
0
        def with_a_template(context):
            context.memoize(
                "target",
                lambda self: StrictMock(template=sample_module.SomeClass))

            context.merge_context("common", fails_if_class_attribute=False)
示例#21
0
 def and_attribute_is_a_property(context):
     context.memoize("attribute", lambda self: "property_attribute")
     context.merge_context("common", fails_if_class_attribute=False)
示例#22
0
 def given_as_a_string(context):
     context.memoize("target", lambda self: "tests.sample_module")
     context.memoize("real_target", lambda self: sample_module)
     context.merge_context("common", fails_if_class_attribute=True)
 def with_magic_method_defined_on_parent_class(context):
     context.memoize("target", lambda self: Target())
     context.merge_context("magic method tests")
示例#24
0
                    def class_methods(context):
                        @context.memoize_before
                        async def method_name(self):
                            return "async_class_method"

                        context.merge_context("async method tests")
    def class_attributes_at_the_class(context):
        @context.memoize
        def class_attribute_target(self):
            return self.get_target_class()

        context.merge_context("class attributes")
                def class_attributes_at_the_instance(context):
                    context.memoize("class_attribute_target",
                                    lambda self: self.target)

                    context.merge_context("class attributes")
示例#27
0
 def with_class_attributes(context):
     context.merge_context("patching works")
        def with_target_mock_memoized(context):
            @context.memoize
            def target_mock(self):
                return self.get_target_mock()

            context.merge_context("StrictMock tests")
示例#29
0
 def given_as_a_reference(context):
     context.memoize("target", lambda self: sample_module)
     context.memoize("real_target", lambda self: self.target)
     context.merge_context("common", fails_if_class_attribute=True)
                    def passes_when(context):

                        context.merge_context("called less times")
                        context.merge_context("called exactly times")