| 1 |
TL;DR |
| 2 |
----- |
| 3 |
|
| 4 |
|
| 5 |
:: |
| 6 |
|
| 7 |
>>> from mockito import * |
| 8 |
>>> myMock = mock() |
| 9 |
>>> when(myMock).getStuff().thenReturn('stuff') |
| 10 |
<mockito.invocation.AnswerSelector object at 0x00CA0BB0> |
| 11 |
>>> myMock.getStuff() |
| 12 |
'stuff' |
| 13 |
>>> verify(myMock).getStuff() |
| 14 |
|
| 15 |
>>> when(myMock).doSomething().thenRaise(Exception('Did a bad thing')) |
| 16 |
<mockito.invocation.AnswerSelector object at 0x00CA0C10> |
| 17 |
>>> myMock.doSomething() |
| 18 |
Traceback (most recent call last): |
| 19 |
<...> |
| 20 |
Exception: Did a bad thing |
| 21 |
|
| 22 |
No difference whatsoever when you mock modules |
| 23 |
|
| 24 |
:: |
| 25 |
|
| 26 |
>>> import os.path |
| 27 |
>>> when(os.path).exists('somewhere/somewhat').thenReturn(True) |
| 28 |
<mockito.invocation.AnswerSelector object at 0x00D394F0> |
| 29 |
>>> when(os.path).exists('somewhere/something').thenReturn(False) |
| 30 |
<mockito.invocation.AnswerSelector object at 0x00C7D1B0> |
| 31 |
>>> os.path.exists('somewhere/somewhat') |
| 32 |
True |
| 33 |
>>> os.path.exists('somewhere/something') |
| 34 |
False |
| 35 |
>>> os.path.exists('another_place') |
| 36 |
Traceback (most recent call last): |
| 37 |
<...> |
| 38 |
mockito.invocation.InvocationError: You called exists with ('another_place',) as |
| 39 |
arguments but we did not expect that. |
| 40 |
|
| 41 |
>>> when(os.path).exist('./somewhat').thenReturn(True) |
| 42 |
Traceback (most recent call last): |
| 43 |
<...> |
| 44 |
mockito.invocation.InvocationError: You tried to stub a method 'exist' |
| 45 |
the object (<module 'ntpath' from ...>) doesn't have. |
| 46 |
|
| 47 |
If that's too strict, you can change it |
| 48 |
|
| 49 |
:: |
| 50 |
|
| 51 |
>>> when(os.path, strict=False).exist('another_place').thenReturn('well, nice here') |
| 52 |
<mockito.invocation.AnswerSelector object at 0x00D429B0> |
| 53 |
>>> os.path.exist('another_place') |
| 54 |
'well, nice here' |
| 55 |
>>> os.path.exist('and here?') |
| 56 |
>>> |
| 57 |
|
| 58 |
No surprise, you can do the same with your classes |
| 59 |
|
| 60 |
:: |
| 61 |
|
| 62 |
>>> class Dog(object): |
| 63 |
... def bark(self): |
| 64 |
... return "Wau" |
| 65 |
... |
| 66 |
>>> when(Dog).bark().thenReturn('Miau!') |
| 67 |
<mockito.invocation.AnswerSelector object at 0x00D42390> |
| 68 |
>>> rex = Dog() |
| 69 |
>>> rex.bark() |
| 70 |
'Miau!' |
| 71 |
|
| 72 |
or just with instances, first unstub |
| 73 |
|
| 74 |
:: |
| 75 |
|
| 76 |
>>> unstub() |
| 77 |
>>> rex.bark() |
| 78 |
'Wau' |
| 79 |
|
| 80 |
then do |
| 81 |
|
| 82 |
:: |
| 83 |
|
| 84 |
>>> when(rex).bark().thenReturn('Grrrrr').thenReturn('Wuff') |
| 85 |
<mockito.invocation.AnswerSelector object at 0x00D48790> |
| 86 |
|
| 87 |
and get something different on consecutive calls |
| 88 |
|
| 89 |
:: |
| 90 |
|
| 91 |
>>> rex.bark() |
| 92 |
'Grrrrr' |
| 93 |
>>> rex.bark() |
| 94 |
'Wuff' |
| 95 |
>>> rex.bark() |
| 96 |
'Wuff' |
| 97 |
|
| 98 |
and since you stubbed an instance, a different instance will not be stubbed |
| 99 |
|
| 100 |
:: |
| 101 |
|
| 102 |
>>> bello = Dog() |
| 103 |
>>> bello.bark() |
| 104 |
'Wau' |
| 105 |
|
| 106 |
You have 4 modifiers when verifying |
| 107 |
|
| 108 |
:: |
| 109 |
|
| 110 |
>>> verify(rex, times=3).bark() |
| 111 |
>>> verify(rex, atleast=1).bark() |
| 112 |
>>> verify(rex, atmost=3).bark() |
| 113 |
>>> verify(rex, between=[1,3]).bark() |
| 114 |
>>> |
| 115 |
|
| 116 |
Finally, we have two matchers |
| 117 |
|
| 118 |
:: |
| 119 |
|
| 120 |
>>> myMock = mock() |
| 121 |
>>> when(myMock).do(any(int)).thenReturn('A number') |
| 122 |
<mockito.invocation.AnswerSelector object at 0x00D394B0> |
| 123 |
>>> when(myMock).do(any(str)).thenReturn('A string') |
| 124 |
<mockito.invocation.AnswerSelector object at 0x00D39E70> |
| 125 |
>>> myMock.do(2) |
| 126 |
'A number' |
| 127 |
>>> myMock.do('times') |
| 128 |
'A string' |
| 129 |
|
| 130 |
>>> verify(myMock).do(any(int)) |
| 131 |
>>> verify(myMock).do(any(str)) |
| 132 |
>>> verify(myMock).do(contains('time')) |
| 133 |
|
| 134 |
>>> exit() |
| 135 |
|
| 136 |
.. toctree:: |
| 137 |
:maxdepth: 2 |
| 138 |
|