1 | n/a | import unittest |
---|
2 | n/a | import sys |
---|
3 | n/a | from unittest.mock import Mock, MagicMock, _magics |
---|
4 | n/a | |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | class TestMockingMagicMethods(unittest.TestCase): |
---|
8 | n/a | |
---|
9 | n/a | def test_deleting_magic_methods(self): |
---|
10 | n/a | mock = Mock() |
---|
11 | n/a | self.assertFalse(hasattr(mock, '__getitem__')) |
---|
12 | n/a | |
---|
13 | n/a | mock.__getitem__ = Mock() |
---|
14 | n/a | self.assertTrue(hasattr(mock, '__getitem__')) |
---|
15 | n/a | |
---|
16 | n/a | del mock.__getitem__ |
---|
17 | n/a | self.assertFalse(hasattr(mock, '__getitem__')) |
---|
18 | n/a | |
---|
19 | n/a | |
---|
20 | n/a | def test_magicmock_del(self): |
---|
21 | n/a | mock = MagicMock() |
---|
22 | n/a | # before using getitem |
---|
23 | n/a | del mock.__getitem__ |
---|
24 | n/a | self.assertRaises(TypeError, lambda: mock['foo']) |
---|
25 | n/a | |
---|
26 | n/a | mock = MagicMock() |
---|
27 | n/a | # this time use it first |
---|
28 | n/a | mock['foo'] |
---|
29 | n/a | del mock.__getitem__ |
---|
30 | n/a | self.assertRaises(TypeError, lambda: mock['foo']) |
---|
31 | n/a | |
---|
32 | n/a | |
---|
33 | n/a | def test_magic_method_wrapping(self): |
---|
34 | n/a | mock = Mock() |
---|
35 | n/a | def f(self, name): |
---|
36 | n/a | return self, 'fish' |
---|
37 | n/a | |
---|
38 | n/a | mock.__getitem__ = f |
---|
39 | n/a | self.assertIsNot(mock.__getitem__, f) |
---|
40 | n/a | self.assertEqual(mock['foo'], (mock, 'fish')) |
---|
41 | n/a | self.assertEqual(mock.__getitem__('foo'), (mock, 'fish')) |
---|
42 | n/a | |
---|
43 | n/a | mock.__getitem__ = mock |
---|
44 | n/a | self.assertIs(mock.__getitem__, mock) |
---|
45 | n/a | |
---|
46 | n/a | |
---|
47 | n/a | def test_magic_methods_isolated_between_mocks(self): |
---|
48 | n/a | mock1 = Mock() |
---|
49 | n/a | mock2 = Mock() |
---|
50 | n/a | |
---|
51 | n/a | mock1.__iter__ = Mock(return_value=iter([])) |
---|
52 | n/a | self.assertEqual(list(mock1), []) |
---|
53 | n/a | self.assertRaises(TypeError, lambda: list(mock2)) |
---|
54 | n/a | |
---|
55 | n/a | |
---|
56 | n/a | def test_repr(self): |
---|
57 | n/a | mock = Mock() |
---|
58 | n/a | self.assertEqual(repr(mock), "<Mock id='%s'>" % id(mock)) |
---|
59 | n/a | mock.__repr__ = lambda s: 'foo' |
---|
60 | n/a | self.assertEqual(repr(mock), 'foo') |
---|
61 | n/a | |
---|
62 | n/a | |
---|
63 | n/a | def test_str(self): |
---|
64 | n/a | mock = Mock() |
---|
65 | n/a | self.assertEqual(str(mock), object.__str__(mock)) |
---|
66 | n/a | mock.__str__ = lambda s: 'foo' |
---|
67 | n/a | self.assertEqual(str(mock), 'foo') |
---|
68 | n/a | |
---|
69 | n/a | |
---|
70 | n/a | def test_dict_methods(self): |
---|
71 | n/a | mock = Mock() |
---|
72 | n/a | |
---|
73 | n/a | self.assertRaises(TypeError, lambda: mock['foo']) |
---|
74 | n/a | def _del(): |
---|
75 | n/a | del mock['foo'] |
---|
76 | n/a | def _set(): |
---|
77 | n/a | mock['foo'] = 3 |
---|
78 | n/a | self.assertRaises(TypeError, _del) |
---|
79 | n/a | self.assertRaises(TypeError, _set) |
---|
80 | n/a | |
---|
81 | n/a | _dict = {} |
---|
82 | n/a | def getitem(s, name): |
---|
83 | n/a | return _dict[name] |
---|
84 | n/a | def setitem(s, name, value): |
---|
85 | n/a | _dict[name] = value |
---|
86 | n/a | def delitem(s, name): |
---|
87 | n/a | del _dict[name] |
---|
88 | n/a | |
---|
89 | n/a | mock.__setitem__ = setitem |
---|
90 | n/a | mock.__getitem__ = getitem |
---|
91 | n/a | mock.__delitem__ = delitem |
---|
92 | n/a | |
---|
93 | n/a | self.assertRaises(KeyError, lambda: mock['foo']) |
---|
94 | n/a | mock['foo'] = 'bar' |
---|
95 | n/a | self.assertEqual(_dict, {'foo': 'bar'}) |
---|
96 | n/a | self.assertEqual(mock['foo'], 'bar') |
---|
97 | n/a | del mock['foo'] |
---|
98 | n/a | self.assertEqual(_dict, {}) |
---|
99 | n/a | |
---|
100 | n/a | |
---|
101 | n/a | def test_numeric(self): |
---|
102 | n/a | original = mock = Mock() |
---|
103 | n/a | mock.value = 0 |
---|
104 | n/a | |
---|
105 | n/a | self.assertRaises(TypeError, lambda: mock + 3) |
---|
106 | n/a | |
---|
107 | n/a | def add(self, other): |
---|
108 | n/a | mock.value += other |
---|
109 | n/a | return self |
---|
110 | n/a | mock.__add__ = add |
---|
111 | n/a | self.assertEqual(mock + 3, mock) |
---|
112 | n/a | self.assertEqual(mock.value, 3) |
---|
113 | n/a | |
---|
114 | n/a | del mock.__add__ |
---|
115 | n/a | def iadd(mock): |
---|
116 | n/a | mock += 3 |
---|
117 | n/a | self.assertRaises(TypeError, iadd, mock) |
---|
118 | n/a | mock.__iadd__ = add |
---|
119 | n/a | mock += 6 |
---|
120 | n/a | self.assertEqual(mock, original) |
---|
121 | n/a | self.assertEqual(mock.value, 9) |
---|
122 | n/a | |
---|
123 | n/a | self.assertRaises(TypeError, lambda: 3 + mock) |
---|
124 | n/a | mock.__radd__ = add |
---|
125 | n/a | self.assertEqual(7 + mock, mock) |
---|
126 | n/a | self.assertEqual(mock.value, 16) |
---|
127 | n/a | |
---|
128 | n/a | def test_division(self): |
---|
129 | n/a | original = mock = Mock() |
---|
130 | n/a | mock.value = 32 |
---|
131 | n/a | self.assertRaises(TypeError, lambda: mock / 2) |
---|
132 | n/a | |
---|
133 | n/a | def truediv(self, other): |
---|
134 | n/a | mock.value /= other |
---|
135 | n/a | return self |
---|
136 | n/a | mock.__truediv__ = truediv |
---|
137 | n/a | self.assertEqual(mock / 2, mock) |
---|
138 | n/a | self.assertEqual(mock.value, 16) |
---|
139 | n/a | |
---|
140 | n/a | del mock.__truediv__ |
---|
141 | n/a | def itruediv(mock): |
---|
142 | n/a | mock /= 4 |
---|
143 | n/a | self.assertRaises(TypeError, itruediv, mock) |
---|
144 | n/a | mock.__itruediv__ = truediv |
---|
145 | n/a | mock /= 8 |
---|
146 | n/a | self.assertEqual(mock, original) |
---|
147 | n/a | self.assertEqual(mock.value, 2) |
---|
148 | n/a | |
---|
149 | n/a | self.assertRaises(TypeError, lambda: 8 / mock) |
---|
150 | n/a | mock.__rtruediv__ = truediv |
---|
151 | n/a | self.assertEqual(0.5 / mock, mock) |
---|
152 | n/a | self.assertEqual(mock.value, 4) |
---|
153 | n/a | |
---|
154 | n/a | def test_hash(self): |
---|
155 | n/a | mock = Mock() |
---|
156 | n/a | # test delegation |
---|
157 | n/a | self.assertEqual(hash(mock), Mock.__hash__(mock)) |
---|
158 | n/a | |
---|
159 | n/a | def _hash(s): |
---|
160 | n/a | return 3 |
---|
161 | n/a | mock.__hash__ = _hash |
---|
162 | n/a | self.assertEqual(hash(mock), 3) |
---|
163 | n/a | |
---|
164 | n/a | |
---|
165 | n/a | def test_nonzero(self): |
---|
166 | n/a | m = Mock() |
---|
167 | n/a | self.assertTrue(bool(m)) |
---|
168 | n/a | |
---|
169 | n/a | m.__bool__ = lambda s: False |
---|
170 | n/a | self.assertFalse(bool(m)) |
---|
171 | n/a | |
---|
172 | n/a | |
---|
173 | n/a | def test_comparison(self): |
---|
174 | n/a | mock = Mock() |
---|
175 | n/a | def comp(s, o): |
---|
176 | n/a | return True |
---|
177 | n/a | mock.__lt__ = mock.__gt__ = mock.__le__ = mock.__ge__ = comp |
---|
178 | n/a | self. assertTrue(mock < 3) |
---|
179 | n/a | self. assertTrue(mock > 3) |
---|
180 | n/a | self. assertTrue(mock <= 3) |
---|
181 | n/a | self. assertTrue(mock >= 3) |
---|
182 | n/a | |
---|
183 | n/a | self.assertRaises(TypeError, lambda: MagicMock() < object()) |
---|
184 | n/a | self.assertRaises(TypeError, lambda: object() < MagicMock()) |
---|
185 | n/a | self.assertRaises(TypeError, lambda: MagicMock() < MagicMock()) |
---|
186 | n/a | self.assertRaises(TypeError, lambda: MagicMock() > object()) |
---|
187 | n/a | self.assertRaises(TypeError, lambda: object() > MagicMock()) |
---|
188 | n/a | self.assertRaises(TypeError, lambda: MagicMock() > MagicMock()) |
---|
189 | n/a | self.assertRaises(TypeError, lambda: MagicMock() <= object()) |
---|
190 | n/a | self.assertRaises(TypeError, lambda: object() <= MagicMock()) |
---|
191 | n/a | self.assertRaises(TypeError, lambda: MagicMock() <= MagicMock()) |
---|
192 | n/a | self.assertRaises(TypeError, lambda: MagicMock() >= object()) |
---|
193 | n/a | self.assertRaises(TypeError, lambda: object() >= MagicMock()) |
---|
194 | n/a | self.assertRaises(TypeError, lambda: MagicMock() >= MagicMock()) |
---|
195 | n/a | |
---|
196 | n/a | |
---|
197 | n/a | def test_equality(self): |
---|
198 | n/a | for mock in Mock(), MagicMock(): |
---|
199 | n/a | self.assertEqual(mock == mock, True) |
---|
200 | n/a | self.assertIsInstance(mock == mock, bool) |
---|
201 | n/a | self.assertEqual(mock != mock, False) |
---|
202 | n/a | self.assertIsInstance(mock != mock, bool) |
---|
203 | n/a | self.assertEqual(mock == object(), False) |
---|
204 | n/a | self.assertEqual(mock != object(), True) |
---|
205 | n/a | |
---|
206 | n/a | def eq(self, other): |
---|
207 | n/a | return other == 3 |
---|
208 | n/a | mock.__eq__ = eq |
---|
209 | n/a | self.assertTrue(mock == 3) |
---|
210 | n/a | self.assertFalse(mock == 4) |
---|
211 | n/a | |
---|
212 | n/a | def ne(self, other): |
---|
213 | n/a | return other == 3 |
---|
214 | n/a | mock.__ne__ = ne |
---|
215 | n/a | self.assertTrue(mock != 3) |
---|
216 | n/a | self.assertFalse(mock != 4) |
---|
217 | n/a | |
---|
218 | n/a | mock = MagicMock() |
---|
219 | n/a | mock.__eq__.return_value = True |
---|
220 | n/a | self.assertIsInstance(mock == 3, bool) |
---|
221 | n/a | self.assertEqual(mock == 3, True) |
---|
222 | n/a | |
---|
223 | n/a | mock.__ne__.return_value = False |
---|
224 | n/a | self.assertIsInstance(mock != 3, bool) |
---|
225 | n/a | self.assertEqual(mock != 3, False) |
---|
226 | n/a | |
---|
227 | n/a | |
---|
228 | n/a | def test_len_contains_iter(self): |
---|
229 | n/a | mock = Mock() |
---|
230 | n/a | |
---|
231 | n/a | self.assertRaises(TypeError, len, mock) |
---|
232 | n/a | self.assertRaises(TypeError, iter, mock) |
---|
233 | n/a | self.assertRaises(TypeError, lambda: 'foo' in mock) |
---|
234 | n/a | |
---|
235 | n/a | mock.__len__ = lambda s: 6 |
---|
236 | n/a | self.assertEqual(len(mock), 6) |
---|
237 | n/a | |
---|
238 | n/a | mock.__contains__ = lambda s, o: o == 3 |
---|
239 | n/a | self.assertIn(3, mock) |
---|
240 | n/a | self.assertNotIn(6, mock) |
---|
241 | n/a | |
---|
242 | n/a | mock.__iter__ = lambda s: iter('foobarbaz') |
---|
243 | n/a | self.assertEqual(list(mock), list('foobarbaz')) |
---|
244 | n/a | |
---|
245 | n/a | |
---|
246 | n/a | def test_magicmock(self): |
---|
247 | n/a | mock = MagicMock() |
---|
248 | n/a | |
---|
249 | n/a | mock.__iter__.return_value = iter([1, 2, 3]) |
---|
250 | n/a | self.assertEqual(list(mock), [1, 2, 3]) |
---|
251 | n/a | |
---|
252 | n/a | getattr(mock, '__bool__').return_value = False |
---|
253 | n/a | self.assertFalse(hasattr(mock, '__nonzero__')) |
---|
254 | n/a | self.assertFalse(bool(mock)) |
---|
255 | n/a | |
---|
256 | n/a | for entry in _magics: |
---|
257 | n/a | self.assertTrue(hasattr(mock, entry)) |
---|
258 | n/a | self.assertFalse(hasattr(mock, '__imaginery__')) |
---|
259 | n/a | |
---|
260 | n/a | |
---|
261 | n/a | def test_magic_mock_equality(self): |
---|
262 | n/a | mock = MagicMock() |
---|
263 | n/a | self.assertIsInstance(mock == object(), bool) |
---|
264 | n/a | self.assertIsInstance(mock != object(), bool) |
---|
265 | n/a | |
---|
266 | n/a | self.assertEqual(mock == object(), False) |
---|
267 | n/a | self.assertEqual(mock != object(), True) |
---|
268 | n/a | self.assertEqual(mock == mock, True) |
---|
269 | n/a | self.assertEqual(mock != mock, False) |
---|
270 | n/a | |
---|
271 | n/a | |
---|
272 | n/a | def test_magicmock_defaults(self): |
---|
273 | n/a | mock = MagicMock() |
---|
274 | n/a | self.assertEqual(int(mock), 1) |
---|
275 | n/a | self.assertEqual(complex(mock), 1j) |
---|
276 | n/a | self.assertEqual(float(mock), 1.0) |
---|
277 | n/a | self.assertNotIn(object(), mock) |
---|
278 | n/a | self.assertEqual(len(mock), 0) |
---|
279 | n/a | self.assertEqual(list(mock), []) |
---|
280 | n/a | self.assertEqual(hash(mock), object.__hash__(mock)) |
---|
281 | n/a | self.assertEqual(str(mock), object.__str__(mock)) |
---|
282 | n/a | self.assertTrue(bool(mock)) |
---|
283 | n/a | |
---|
284 | n/a | # in Python 3 oct and hex use __index__ |
---|
285 | n/a | # so these tests are for __index__ in py3k |
---|
286 | n/a | self.assertEqual(oct(mock), '0o1') |
---|
287 | n/a | self.assertEqual(hex(mock), '0x1') |
---|
288 | n/a | # how to test __sizeof__ ? |
---|
289 | n/a | |
---|
290 | n/a | |
---|
291 | n/a | def test_magic_methods_and_spec(self): |
---|
292 | n/a | class Iterable(object): |
---|
293 | n/a | def __iter__(self): |
---|
294 | n/a | pass |
---|
295 | n/a | |
---|
296 | n/a | mock = Mock(spec=Iterable) |
---|
297 | n/a | self.assertRaises(AttributeError, lambda: mock.__iter__) |
---|
298 | n/a | |
---|
299 | n/a | mock.__iter__ = Mock(return_value=iter([])) |
---|
300 | n/a | self.assertEqual(list(mock), []) |
---|
301 | n/a | |
---|
302 | n/a | class NonIterable(object): |
---|
303 | n/a | pass |
---|
304 | n/a | mock = Mock(spec=NonIterable) |
---|
305 | n/a | self.assertRaises(AttributeError, lambda: mock.__iter__) |
---|
306 | n/a | |
---|
307 | n/a | def set_int(): |
---|
308 | n/a | mock.__int__ = Mock(return_value=iter([])) |
---|
309 | n/a | self.assertRaises(AttributeError, set_int) |
---|
310 | n/a | |
---|
311 | n/a | mock = MagicMock(spec=Iterable) |
---|
312 | n/a | self.assertEqual(list(mock), []) |
---|
313 | n/a | self.assertRaises(AttributeError, set_int) |
---|
314 | n/a | |
---|
315 | n/a | |
---|
316 | n/a | def test_magic_methods_and_spec_set(self): |
---|
317 | n/a | class Iterable(object): |
---|
318 | n/a | def __iter__(self): |
---|
319 | n/a | pass |
---|
320 | n/a | |
---|
321 | n/a | mock = Mock(spec_set=Iterable) |
---|
322 | n/a | self.assertRaises(AttributeError, lambda: mock.__iter__) |
---|
323 | n/a | |
---|
324 | n/a | mock.__iter__ = Mock(return_value=iter([])) |
---|
325 | n/a | self.assertEqual(list(mock), []) |
---|
326 | n/a | |
---|
327 | n/a | class NonIterable(object): |
---|
328 | n/a | pass |
---|
329 | n/a | mock = Mock(spec_set=NonIterable) |
---|
330 | n/a | self.assertRaises(AttributeError, lambda: mock.__iter__) |
---|
331 | n/a | |
---|
332 | n/a | def set_int(): |
---|
333 | n/a | mock.__int__ = Mock(return_value=iter([])) |
---|
334 | n/a | self.assertRaises(AttributeError, set_int) |
---|
335 | n/a | |
---|
336 | n/a | mock = MagicMock(spec_set=Iterable) |
---|
337 | n/a | self.assertEqual(list(mock), []) |
---|
338 | n/a | self.assertRaises(AttributeError, set_int) |
---|
339 | n/a | |
---|
340 | n/a | |
---|
341 | n/a | def test_setting_unsupported_magic_method(self): |
---|
342 | n/a | mock = MagicMock() |
---|
343 | n/a | def set_setattr(): |
---|
344 | n/a | mock.__setattr__ = lambda self, name: None |
---|
345 | n/a | self.assertRaisesRegex(AttributeError, |
---|
346 | n/a | "Attempting to set unsupported magic method '__setattr__'.", |
---|
347 | n/a | set_setattr |
---|
348 | n/a | ) |
---|
349 | n/a | |
---|
350 | n/a | |
---|
351 | n/a | def test_attributes_and_return_value(self): |
---|
352 | n/a | mock = MagicMock() |
---|
353 | n/a | attr = mock.foo |
---|
354 | n/a | def _get_type(obj): |
---|
355 | n/a | # the type of every mock (or magicmock) is a custom subclass |
---|
356 | n/a | # so the real type is the second in the mro |
---|
357 | n/a | return type(obj).__mro__[1] |
---|
358 | n/a | self.assertEqual(_get_type(attr), MagicMock) |
---|
359 | n/a | |
---|
360 | n/a | returned = mock() |
---|
361 | n/a | self.assertEqual(_get_type(returned), MagicMock) |
---|
362 | n/a | |
---|
363 | n/a | |
---|
364 | n/a | def test_magic_methods_are_magic_mocks(self): |
---|
365 | n/a | mock = MagicMock() |
---|
366 | n/a | self.assertIsInstance(mock.__getitem__, MagicMock) |
---|
367 | n/a | |
---|
368 | n/a | mock[1][2].__getitem__.return_value = 3 |
---|
369 | n/a | self.assertEqual(mock[1][2][3], 3) |
---|
370 | n/a | |
---|
371 | n/a | |
---|
372 | n/a | def test_magic_method_reset_mock(self): |
---|
373 | n/a | mock = MagicMock() |
---|
374 | n/a | str(mock) |
---|
375 | n/a | self.assertTrue(mock.__str__.called) |
---|
376 | n/a | mock.reset_mock() |
---|
377 | n/a | self.assertFalse(mock.__str__.called) |
---|
378 | n/a | |
---|
379 | n/a | |
---|
380 | n/a | def test_dir(self): |
---|
381 | n/a | # overriding the default implementation |
---|
382 | n/a | for mock in Mock(), MagicMock(): |
---|
383 | n/a | def _dir(self): |
---|
384 | n/a | return ['foo'] |
---|
385 | n/a | mock.__dir__ = _dir |
---|
386 | n/a | self.assertEqual(dir(mock), ['foo']) |
---|
387 | n/a | |
---|
388 | n/a | |
---|
389 | n/a | @unittest.skipIf('PyPy' in sys.version, "This fails differently on pypy") |
---|
390 | n/a | def test_bound_methods(self): |
---|
391 | n/a | m = Mock() |
---|
392 | n/a | |
---|
393 | n/a | # XXXX should this be an expected failure instead? |
---|
394 | n/a | |
---|
395 | n/a | # this seems like it should work, but is hard to do without introducing |
---|
396 | n/a | # other api inconsistencies. Failure message could be better though. |
---|
397 | n/a | m.__iter__ = [3].__iter__ |
---|
398 | n/a | self.assertRaises(TypeError, iter, m) |
---|
399 | n/a | |
---|
400 | n/a | |
---|
401 | n/a | def test_magic_method_type(self): |
---|
402 | n/a | class Foo(MagicMock): |
---|
403 | n/a | pass |
---|
404 | n/a | |
---|
405 | n/a | foo = Foo() |
---|
406 | n/a | self.assertIsInstance(foo.__int__, Foo) |
---|
407 | n/a | |
---|
408 | n/a | |
---|
409 | n/a | def test_descriptor_from_class(self): |
---|
410 | n/a | m = MagicMock() |
---|
411 | n/a | type(m).__str__.return_value = 'foo' |
---|
412 | n/a | self.assertEqual(str(m), 'foo') |
---|
413 | n/a | |
---|
414 | n/a | |
---|
415 | n/a | def test_iterable_as_iter_return_value(self): |
---|
416 | n/a | m = MagicMock() |
---|
417 | n/a | m.__iter__.return_value = [1, 2, 3] |
---|
418 | n/a | self.assertEqual(list(m), [1, 2, 3]) |
---|
419 | n/a | self.assertEqual(list(m), [1, 2, 3]) |
---|
420 | n/a | |
---|
421 | n/a | m.__iter__.return_value = iter([4, 5, 6]) |
---|
422 | n/a | self.assertEqual(list(m), [4, 5, 6]) |
---|
423 | n/a | self.assertEqual(list(m), []) |
---|
424 | n/a | |
---|
425 | n/a | |
---|
426 | n/a | def test_matmul(self): |
---|
427 | n/a | m = MagicMock() |
---|
428 | n/a | self.assertIsInstance(m @ 1, MagicMock) |
---|
429 | n/a | m.__matmul__.return_value = 42 |
---|
430 | n/a | m.__rmatmul__.return_value = 666 |
---|
431 | n/a | m.__imatmul__.return_value = 24 |
---|
432 | n/a | self.assertEqual(m @ 1, 42) |
---|
433 | n/a | self.assertEqual(1 @ m, 666) |
---|
434 | n/a | m @= 24 |
---|
435 | n/a | self.assertEqual(m, 24) |
---|
436 | n/a | |
---|
437 | n/a | def test_divmod_and_rdivmod(self): |
---|
438 | n/a | m = MagicMock() |
---|
439 | n/a | self.assertIsInstance(divmod(5, m), MagicMock) |
---|
440 | n/a | m.__divmod__.return_value = (2, 1) |
---|
441 | n/a | self.assertEqual(divmod(m, 2), (2, 1)) |
---|
442 | n/a | m = MagicMock() |
---|
443 | n/a | foo = divmod(2, m) |
---|
444 | n/a | self.assertIsInstance(foo, MagicMock) |
---|
445 | n/a | foo_direct = m.__divmod__(2) |
---|
446 | n/a | self.assertIsInstance(foo_direct, MagicMock) |
---|
447 | n/a | bar = divmod(m, 2) |
---|
448 | n/a | self.assertIsInstance(bar, MagicMock) |
---|
449 | n/a | bar_direct = m.__rdivmod__(2) |
---|
450 | n/a | self.assertIsInstance(bar_direct, MagicMock) |
---|
451 | n/a | |
---|
452 | n/a | # http://bugs.python.org/issue23310 |
---|
453 | n/a | # Check if you can change behaviour of magic methds in MagicMock init |
---|
454 | n/a | def test_magic_in_initialization(self): |
---|
455 | n/a | m = MagicMock(**{'__str__.return_value': "12"}) |
---|
456 | n/a | self.assertEqual(str(m), "12") |
---|
457 | n/a | |
---|
458 | n/a | def test_changing_magic_set_in_initialization(self): |
---|
459 | n/a | m = MagicMock(**{'__str__.return_value': "12"}) |
---|
460 | n/a | m.__str__.return_value = "13" |
---|
461 | n/a | self.assertEqual(str(m), "13") |
---|
462 | n/a | m = MagicMock(**{'__str__.return_value': "12"}) |
---|
463 | n/a | m.configure_mock(**{'__str__.return_value': "14"}) |
---|
464 | n/a | self.assertEqual(str(m), "14") |
---|
465 | n/a | |
---|
466 | n/a | |
---|
467 | n/a | if __name__ == '__main__': |
---|
468 | n/a | unittest.main() |
---|