Skip to content

Spying context managers #9

@bifer

Description

@bifer

Hi David,

is there any way to test contextmanager aware classes? Let me show you this example:

import unittest
from doublex import Spy, called, assert_that


class Context():
    def __init__(self):
        pass

    def __enter__(self):
        return self

    def __exit__(self, exception_type, exception_value, exception_traceback):
        pass

    def execute(self):
        print("foo")

context = Context()

def main(context):
    with context as mycontext:
        mycontext.execute()


class TestContext(unittest.TestCase):

    def testing_context(self):
        context = Spy()

        main(context)

        assert_that(context.execute, called())


if __name__ == '__main__':
    unittest.main()

If you run this test, it fails. Seems as you can use doublex (Spy in this case) as context managers, you cannot spy a class using this special methods (__enter__() and __exit__()).
The line in conflict is this:

self._setting_up = True
but I suppose it is there to use doublex as context manager.

So the question... is there any way to test this? I haven't find anything related in docs.

Regards and thanks in advanced!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions