I've been strugging a bit testing an open
call with the doubles
library. As is the case many times, most
of my struggle has revolved aroudn writing python2 and 3 compatible code/tests.
I'm trying to assert the built in open
function is called with the right file name argument.
The first thing you need to do is disable builtin verification in the doubles
library, using a context manager
they provide. Next, you need to handle the change in modules
where open is provided from 2 to 3, __builtin__
to builtins
. More documentation available
here. And finally, the differences in StringIO
-
see more here.
The final version of my test to check open
is called with the correct arguments, looks like this:
def test_io():
with no_builtin_verification():
i = __import__(('builtins' if sys.version_info >= (3,)
else '__builtin__'))
expect(i).open.with_args(Config().config_file, 'w+').and_return(
io.BytesIO()
)