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")
예제 #6
0
        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")
예제 #8
0
    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")
예제 #9
0
                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)
예제 #10
0
                    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")
예제 #11
0
    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")
예제 #12
0
    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)
예제 #13
0
        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,
                                    )
예제 #15
0
        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")
예제 #16
0
    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")
예제 #17
0
        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")
예제 #18
0
                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)
예제 #23
0
 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)
예제 #30
0
                    def passes_when(context):

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