@@ -241,7 +241,25 @@ def test_save_to_stdout(self):
241241
242242 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
243243
244- with mock .patch ('sys.stdout' , new = six .BytesIO ()) as fake_stdout :
244+ class FakeStdout (six .BytesIO ):
245+ def __init__ (self ):
246+ six .BytesIO .__init__ (self )
247+ self .context_manager_calls = []
248+
249+ def __enter__ (self ):
250+ self .context_manager_calls .append ('__enter__' )
251+ return self
252+
253+ def __exit__ (self , * a ):
254+ self .context_manager_calls .append ('__exit__' )
255+
256+ with mock .patch ('sys.stdout' ) as fake_stdout , mock .patch (
257+ 'os.fdopen' , return_value = FakeStdout ()) as fake_fdopen :
258+ fake_stdout .fileno .return_value = 123
245259 self .cmd .take_action (parsed_args )
246260
247- self .assertEqual (fake_stdout .getvalue (), object_fakes .object_1_content )
261+ self .assertEqual (fake_fdopen .return_value .getvalue (),
262+ object_fakes .object_1_content )
263+ self .assertEqual (fake_fdopen .mock_calls , [mock .call (123 , 'wb' )])
264+ self .assertEqual (fake_fdopen .return_value .context_manager_calls ,
265+ ['__enter__' , '__exit__' ])
0 commit comments