File: unstub_test.py

package info (click to toggle)
python-mockito 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 528 kB
  • sloc: python: 4,102; makefile: 206
file content (138 lines) | stat: -rw-r--r-- 3,741 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import pytest

from mockito import mock, when, unstub, verify, ArgumentError


class Dog(object):
    def waggle(self):
        return 'Unsure'

    def bark(self, sound='Wuff'):
        return sound


class TestUntub:
    def testIndependentUnstubbing(self):
        rex = Dog()
        mox = Dog()

        when(rex).waggle().thenReturn('Yup')
        when(mox).waggle().thenReturn('Nope')

        assert rex.waggle() == 'Yup'
        assert mox.waggle() == 'Nope'

        unstub(rex)

        assert rex.waggle() == 'Unsure'
        assert mox.waggle() == 'Nope'

        unstub(mox)

        assert mox.waggle() == 'Unsure'

    def testUnconfigureMock(self):
        m = mock()
        when(m).foo().thenReturn(42)
        assert m.foo() == 42
        unstub(m)
        assert m.foo() is None

class TestAutomaticUnstubbing:

    def testWith1(self):
        rex = Dog()

        with when(rex).waggle().thenReturn('Yup'):
            assert rex.waggle() == 'Yup'
            verify(rex).waggle()

        assert rex.waggle() == 'Unsure'

    def testWith2(self):
        # Ensure the short version to return None works
        rex = Dog()

        with when(rex).waggle():
            assert rex.waggle() is None
            verify(rex).waggle()

        assert rex.waggle() == 'Unsure'

    def testWithRaisingSideeffect(self):
        rex = Dog()

        with pytest.raises(RuntimeError):
            with when(rex).waggle().thenRaise(RuntimeError('Nope')):
                rex.waggle()
            assert rex.waggle() == 'Unsure'

    def testNesting(self):
        # That's not a real test, I just wanted to see how it would
        # look like; bc you run out of space pretty quickly
        rex = Dog()
        mox = Dog()

        with when(rex).waggle().thenReturn('Yup'):
            with when(mox).waggle().thenReturn('Nope'):
                assert rex.waggle() == 'Yup'

        assert rex.waggle() == 'Unsure'
        assert mox.waggle() == 'Unsure'
        # though that's a good looking option
        with when(rex).waggle().thenReturn('Yup'), \
             when(mox).waggle().thenReturn('Nope'):  # noqa: E127
            assert rex.waggle() == 'Yup'
            assert mox.waggle() == 'Nope'

        assert rex.waggle() == 'Unsure'
        assert mox.waggle() == 'Unsure'

    def testOnlyUnstubTheExactStub(self):
        rex = Dog()

        when(rex).bark('Shhh').thenReturn('Nope')
        with when(rex).bark('Miau').thenReturn('Grrr'):
            assert rex.bark('Miau') == 'Grrr'

        assert rex.bark('Shhh') == 'Nope'
        verify(rex, times=2).bark(Ellipsis)

    def testOnlyUnstubTheExcatMethod(self):
        rex = Dog()

        when(rex).bark('Shhh').thenReturn('Nope')
        with when(rex).waggle().thenReturn('Yup'):
            assert rex.waggle() == 'Yup'

        assert rex.bark('Shhh') == 'Nope'
        verify(rex, times=1).bark(Ellipsis)
        verify(rex, times=1).waggle()

    def testCleanupRegistryAfterLastStub(self):
        rex = Dog()

        with when(rex).bark('Shhh').thenReturn('Nope'):
            with when(rex).bark('Miau').thenReturn('Grrr'):
                assert rex.bark('Miau') == 'Grrr'
            assert rex.bark('Shhh') == 'Nope'

        with pytest.raises(ArgumentError):
            verify(rex).bark(Ellipsis)


    class TestEnsureCleanUnstubIfMockingAGlobal:
        def testA(self):
            with when(Dog).waggle().thenReturn('Sure'):
                rex = Dog()
                assert rex.waggle() == 'Sure'

                verify(Dog).waggle()

        def testB(self):
            with when(Dog).waggle().thenReturn('Sure'):
                rex = Dog()
                assert rex.waggle() == 'Sure'

                verify(Dog).waggle()