File: test_context_managers.py

package info (click to toggle)
python-kgb 7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 532 kB
  • sloc: python: 4,466; makefile: 3
file content (33 lines) | stat: -rw-r--r-- 933 bytes parent folder | download | duplicates (3)
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
from __future__ import unicode_literals

from kgb.contextmanagers import spy_on
from kgb.tests.base import MathClass, TestCase


class SpyOnTests(TestCase):
    """Unit tests for spies.contextmanagers.spy_on."""

    def test_spy_on(self):
        """Testing spy_on context manager"""
        obj = MathClass()

        with spy_on(obj.do_math):
            self.assertTrue(hasattr(obj.do_math, 'spy'))

            result = obj.do_math()
            self.assertEqual(result, 3)

        self.assertFalse(hasattr(obj.do_math, 'spy'))

    def test_expose_spy(self):
        """Testing spy_on exposes `spy` via context manager"""
        obj = MathClass()

        with spy_on(obj.do_math) as spy:
            self.assertTrue(hasattr(obj.do_math, 'spy'))
            self.assertIs(obj.do_math.spy, spy)

            result = obj.do_math()
            self.assertEqual(result, 3)

        self.assertFalse(hasattr(obj.do_math, 'spy'))