示例#1
0
    def test_fails_when_called_once(self):
        subject = InstanceDouble('doubles.testing.User')

        expect(subject).instance_method.twice()

        subject.instance_method()
        with raises(MockExpectationError) as e:
            verify()
        teardown()

        assert re.match(
            r"Expected 'instance_method' to be called 2 times instead of 1 time on "
            r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> "
            r"with any args, but was not."
            r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value))
示例#2
0
    def test_raises_if_an_expected_method_call_with_args_is_not_made(self):
        subject = InstanceDouble('doubles.testing.User')

        expect(subject).method_with_varargs.with_args('bar')

        with raises(MockExpectationError) as e:
            verify()
        teardown()

        assert re.match(
            r"Expected 'method_with_varargs' to be called on "
            r"<InstanceDouble of <class '?doubles.testing.User'?"
            r"(?: at 0x[0-9a-f]{9})?> object at .+> "
            r"with \('bar'\), but was not."
            r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value))
示例#3
0
    def test_fails_when_called_more_than_at_most_times(self):
        subject = InstanceDouble('doubles.testing.User')

        expect(subject).instance_method.at_most(1).times

        subject.instance_method()
        with raises(MockExpectationError) as e:
            subject.instance_method()
        teardown()

        assert re.match(
            r"Expected 'instance_method' to be called at most 1 time instead of 2 times on "
            r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> "
            r"with any args, but was not."
            r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value))
示例#4
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))
示例#5
0
    def test_fails_when_called_two_times(self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).instance_method.once()

        subject.instance_method()
        with raises(MockExpectationError) as e:
            subject.instance_method()
        teardown()

        assert re.match(
            r"Allowed 'instance_method' to be called 1 time instead of 2 times on "
            r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> "
            r"with any args, but was not."
            r" \(.*doubles/test/allow_test.py:\d+\)", str(e.value))
示例#6
0
    def test_unsatisfied_expectation(self):
        subject = InstanceDouble('doubles.testing.User')

        expect(subject).__exit__.once()

        with raises(MockExpectationError) as e:
            verify()
        teardown()

        assert re.match(
            r"Expected '__exit__' to be called 1 time instead of 0 times on "
            r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> "
            r"with any args, but was not."
            r" \(.*doubles/test/expect_test.py:\d+\)",
            str(e.value)
        )
示例#7
0
    def test_with_args_validator_not_called(self):
        subject = InstanceDouble('doubles.testing.User')

        def arg_matcher(*args):
            return True

        expect(subject).method_with_varargs.with_args_validator(arg_matcher)
        with raises(MockExpectationError) as e:
            verify()
        teardown()

        assert re.match(
            r"Expected 'method_with_varargs' to be called on "
            r"<InstanceDouble of <class '?doubles.testing.User'?"
            r"(?: at 0x[0-9a-f]{9})?> object at .+> "
            r"with custom matcher: 'arg_matcher', but was not."
            r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value))
示例#8
0
 def test_proxies_can_be_inspected(self):
     subject = InstanceDouble("doubles.testing.User")
     allow(subject).instance_method
     parameters = inspect.signature(subject.instance_method).parameters
     assert len(parameters) == 1
示例#9
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'
示例#10
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())
示例#11
0
    def test_proxies_name(self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).method_with_doc

        assert subject.method_with_doc.__name__ == "method_with_doc"
示例#12
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
示例#13
0
    def test_returns_specified_value(self, stubber):
        subject = InstanceDouble('doubles.testing.User')

        stubber(subject).instance_method.and_return('bar')

        assert subject.instance_method() == 'bar'
示例#14
0
    def test_passes_when_called_less_than_at_most_times(self):
        subject = InstanceDouble('doubles.testing.User')

        expect(subject).instance_method.at_most(2).times

        subject.instance_method()
示例#15
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
示例#16
0
    def test_passes_when_called_exactly_expected_times(self):
        subject = InstanceDouble('doubles.testing.User')

        expect(subject).instance_method.exactly(1).times

        subject.instance_method()
示例#17
0
    def test_passes_when_called_once(self):
        subject = InstanceDouble('doubles.testing.User')

        expect(subject).instance_method.once()

        subject.instance_method()
示例#18
0
    def test_allows_method_calls_on_doubles(self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).instance_method

        assert subject.instance_method() is None
示例#19
0
    def test_chains_with_return_values(self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).instance_method.with_no_args().and_return('bar')

        assert subject.instance_method() == 'bar'
示例#20
0
    def test_allows_call_with_no_arguments(self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).instance_method.with_no_args()

        assert subject.instance_method() is None
示例#21
0
    def test_passes_when_called_never(self):
        subject = InstanceDouble('doubles.testing.User')

        expect(subject).instance_method.never()
示例#22
0
    def test_raises_on_undefined_attribute_access(self):
        subject = InstanceDouble('doubles.testing.User')

        with raises(AttributeError):
            subject.instance_method
示例#23
0
    def test_calls_are_chainable(self):
        subject = InstanceDouble('doubles.testing.User')

        expect(subject).instance_method.at_least(2).times.at_least(1).times

        subject.instance_method()
示例#24
0
    def test_raises_if_called_with_args_that_do_not_match_signature(self):
        subject = InstanceDouble('doubles.testing.User')
        allow(subject).instance_method

        with raises(VerifyingDoubleArgumentError):
            subject.instance_method('bar')
示例#25
0
    def test_satisfied_expectation(self):
        subject = InstanceDouble('doubles.testing.User')

        expect(subject).__call__.once()

        subject()
示例#26
0
    def test_called_with_zero(self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).instance_method.at_most(0).times
示例#27
0
    def test_passes_if_an_expected_method_call_is_made(self):
        subject = InstanceDouble('doubles.testing.User')

        expect(subject).instance_method

        subject.instance_method()
示例#28
0
    def test_passes_when_called_exactly_at_most_times(self):
        subject = InstanceDouble('doubles.testing.User')

        allow(subject).instance_method.at_most(1).times

        subject.instance_method()
示例#29
0
                def runTest(self):
                    subject = InstanceDouble('doubles.testing.User')

                    expect(subject).instance_method
示例#30
0
 def setup(self):
     self.subject = InstanceDouble('doubles.testing.User')