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

        allow(subject).method_with_positional_arguments.and_return('bar')

        assert subject.method_with_positional_arguments(
            'unspecified argument') == 'bar'
Exemple #2
0
    def test_returns_result_of_a_callable_with_positional_arg(self, stubber):
        subject = InstanceDouble('doubles.testing.User')

        stubber(subject).method_with_positional_arguments.and_return_result_of(
            lambda x: x)

        assert subject.method_with_positional_arguments('bar') == 'bar'
Exemple #3
0
    def test__call__is_short_hand_for_with_args(self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).method_with_positional_arguments('Bob').and_return(
            'Barker')
        assert subject.method_with_positional_arguments('Bob') == 'Barker'
Exemple #4
0
    def test_objects_with_custom__eq__method_two(self):
        subject = InstanceDouble('doubles.testing.User')
        allow(subject).method_with_positional_arguments.with_args(
            AlwaysEquals())

        subject.method_with_positional_arguments(NeverEquals())
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
    def test_allows_specification_of_arguments(self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).method_with_positional_arguments.with_args('foo')

        assert subject.method_with_positional_arguments('foo') is None
Exemple #7
0
    def test_returns_result_of_a_callable_with_positional_arg(self, stubber):
        subject = InstanceDouble('doubles.testing.User')

        stubber(subject).method_with_positional_arguments.and_return_result_of(lambda x: x)

        assert subject.method_with_positional_arguments('bar') == 'bar'
Exemple #8
0
    def test_allows_specification_of_arguments(self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).method_with_positional_arguments.with_args('foo')

        assert subject.method_with_positional_arguments('foo') is None
Exemple #9
0
    def test_allows_any_arguments_if_none_are_specified(self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).method_with_positional_arguments.and_return('bar')

        assert subject.method_with_positional_arguments('unspecified argument') == 'bar'
Exemple #10
0
    def test__call__is_short_hand_for_with_args(self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).method_with_positional_arguments('Bob').and_return('Barker')
        assert subject.method_with_positional_arguments('Bob') == 'Barker'
Exemple #11
0
    def test_objects_with_custom__eq__method_two(self):
        subject = InstanceDouble('doubles.testing.User')
        allow(subject).method_with_positional_arguments.with_args(AlwaysEquals())

        subject.method_with_positional_arguments(NeverEquals())
Exemple #12
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')