Exemple #1
0
    def test_raises_if_arguments_were_specified_but_wrong_kwarg_used_when_called(
            self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).method_with_default_args.with_args('one', bar='two')

        with raises(UnallowedMethodCallError) as e:
            subject.method_with_default_args('one', bob='barker')

        assert re.match(
            r"Received unexpected call to 'method_with_default_args' on "
            r"<InstanceDouble of <class '?doubles.testing.User'?"
            r"(?: at 0x[0-9a-f]{9})?> object at .+>\."
            r"  The supplied arguments \('one', bob='barker'\)"
            r" do not match any available allowances.", str(e.value))
Exemple #2
0
    def test_raises_if_arguments_were_specified_but_not_provided_when_called(self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).method_with_default_args.with_args('one', bar='two')

        with raises(UnallowedMethodCallError) as e:
            subject.method_with_default_args()

        assert re.match(
            r"Received unexpected call to 'method_with_default_args' on "
            r"<InstanceDouble of <class '?doubles.testing.User'?"
            r"(?: at 0x[0-9a-f]{9})?> object at .+>\."
            r"  The supplied arguments \(\)"
            r" do not match any available allowances.",
            str(e.value)
        )
Exemple #3
0
    def test_passes_if_method_is_called_with_specified_arguments(self):
        subject = InstanceDouble('doubles.testing.User')

        expect(subject).method_with_default_args.with_args('one', bar='two')

        assert subject.method_with_default_args('one', bar='two') is None
Exemple #4
0
    def test_passes_if_method_is_called_with_specified_arguments(self):
        subject = InstanceDouble('doubles.testing.User')

        expect(subject).method_with_default_args.with_args('one', bar='two')

        assert subject.method_with_default_args('one', bar='two') is None
Exemple #5
0
class TestCustomMatcher(object):
    def setup(self):
        self.subject = InstanceDouble('doubles.testing.User')

    def test_matcher_raises_an_exception(self):
        def func():
            raise Exception('Bob Barker')

        allow(self.subject).instance_method.with_args_validator(func)
        with raises(UnallowedMethodCallError):
            self.subject.instance_method()

    def test_matcher_with_no_args_returns_true(self):
        allow(self.subject).instance_method.with_args_validator(
            lambda: True).and_return('Bob')
        self.subject.instance_method() == 'Bob'

    def test_matcher_with_no_args_returns_false(self):
        allow(self.subject).instance_method.with_args_validator(lambda: False)
        with raises(UnallowedMethodCallError):
            self.subject.instance_method()

    def test_matcher_with_positional_args_returns_true(self):
        (allow(
            self.subject).method_with_positional_arguments.with_args_validator(
                lambda x: True).and_return('Bob'))
        self.subject.method_with_positional_arguments('Bob Barker') == 'Bob'

    def test_matcher_with_positional_args_returns_false(self):
        allow(
            self.subject).method_with_positional_arguments.with_args_validator(
                lambda x: False)
        with raises(UnallowedMethodCallError):
            self.subject.method_with_positional_arguments('Bob Barker')

    def test_matcher_with_kwargs_args_returns_false(self):
        def func(bar=None):
            return False

        allow(self.subject).instance_method.with_args_validator(func)
        with raises(UnallowedMethodCallError):
            self.subject.instance_method()

    def test_matcher_with_kwargs_args_returns_true(self):
        def func(bar=None):
            return True

        allow(self.subject).instance_method.with_args_validator(
            func).and_return('Bob')
        self.subject.instance_method() == 'Bob'

    def test_matcher_with_positional_and_kwargs_returns_true(self):
        def func(foo, bar=None):
            return True

        allow(self.subject).method_with_default_args.with_args_validator(
            func).and_return('Bob')
        self.subject.method_with_default_args('bob', bar='Barker') == 'Bob'

    def test_matcher_with_positional_and_kwargs_returns_false(self):
        def func(foo, bar=None):
            return False

        allow(self.subject).method_with_default_args.with_args_validator(
            func).and_return('Bob')
        with raises(UnallowedMethodCallError):
            self.subject.method_with_default_args('bob', bar='Barker')

    def test_matcher_returns_true_but_args_do_not_match_call_signature(self):
        allow(self.subject).instance_method.with_args_validator(lambda x: True)
        with raises(VerifyingDoubleArgumentError):
            self.subject.instance_method('bob')
Exemple #6
0
class TestCustomMatcher(object):
    def setup(self):
        self.subject = InstanceDouble('doubles.testing.User')

    def test_matcher_raises_an_exception(self):
        def func():
            raise Exception('Bob Barker')

        allow(self.subject).instance_method.with_args_validator(func)
        with raises(UnallowedMethodCallError):
            self.subject.instance_method()

    def test_matcher_with_no_args_returns_true(self):
        allow(self.subject).instance_method.with_args_validator(lambda: True).and_return('Bob')
        self.subject.instance_method() == 'Bob'

    def test_matcher_with_no_args_returns_false(self):
        allow(self.subject).instance_method.with_args_validator(lambda: False)
        with raises(UnallowedMethodCallError):
            self.subject.instance_method()

    def test_matcher_with_positional_args_returns_true(self):
        (allow(self.subject)
            .method_with_positional_arguments
            .with_args_validator(lambda x: True)
            .and_return('Bob'))
        self.subject.method_with_positional_arguments('Bob Barker') == 'Bob'

    def test_matcher_with_positional_args_returns_false(self):
        allow(self.subject).method_with_positional_arguments.with_args_validator(lambda x: False)
        with raises(UnallowedMethodCallError):
            self.subject.method_with_positional_arguments('Bob Barker')

    def test_matcher_with_kwargs_args_returns_false(self):
        def func(bar=None):
            return False
        allow(self.subject).instance_method.with_args_validator(func)
        with raises(UnallowedMethodCallError):
            self.subject.instance_method()

    def test_matcher_with_kwargs_args_returns_true(self):
        def func(bar=None):
            return True
        allow(self.subject).instance_method.with_args_validator(func).and_return('Bob')
        self.subject.instance_method() == 'Bob'

    def test_matcher_with_positional_and_kwargs_returns_true(self):
        def func(foo, bar=None):
            return True
        allow(self.subject).method_with_default_args.with_args_validator(func).and_return('Bob')
        self.subject.method_with_default_args('bob', bar='Barker') == 'Bob'

    def test_matcher_with_positional_and_kwargs_returns_false(self):
        def func(foo, bar=None):
            return False
        allow(self.subject).method_with_default_args.with_args_validator(func).and_return('Bob')
        with raises(UnallowedMethodCallError):
            self.subject.method_with_default_args('bob', bar='Barker')

    def test_matcher_returns_true_but_args_do_not_match_call_signature(self):
        allow(self.subject).instance_method.with_args_validator(lambda x: True)
        with raises(VerifyingDoubleArgumentError):
            self.subject.instance_method('bob')