def mock_cast_as_call(obj=None):
 orig_prepare = obj.prepare
 def prepare(*args, **kwargs):
  cctxt = orig_prepare(*args, **kwargs)
  mock_cast_as_call(obj=cctxt)
  return cctxt
 prepare_patch = mock.patch.object(obj, 'prepare').start()
 prepare_patch.side_effect = prepare
 cast_patch = mock.patch.object(obj, 'cast').start()
 cast_patch.side_effect = obj.call

def mock_cast_as_call ( obj = None ) : orig_prepare = obj . prepare def prepare ( * args , ** kwargs ) : cctxt = orig_prepare ( * args , ** kwargs ) mock_cast_as_call ( obj = cctxt ) return cctxt prepare_patch = mock . patch . object ( obj , 'prepare' ) . start ( ) prepare_patch . side_effect = prepare cast_patch = mock . patch . object ( obj , 'cast' ) . start ( ) cast_patch . side_effect = obj . call
use this to mock cast as calls .