我可以在自定义错误处理程序中模拟替换的异常吗?

时间:2016-05-26 20:33:31

标签: python mocking urllib2 python-mock

尝试使用$http.get('http://localhost:3000/+' + $scope.myQuery).then(function(){ //... }); 中的patch编写单元测试来验证我的自定义mock错误处理程序,该错误处理程序在引发自定义错误之前记录错误,我无法获得自定义错误到urllib2raise中的日志行成功执行后,模拟的异常将始终为raise。即我的测试代码正在寻找LoggingErrorHandler,但令人惊讶的是,该功能仍会提升CustomError

测试代码:

urllib2.HTTPError

urllib2 HTTPError处理的自定义错误处理程序:

@raises(CustomError)
def test_404_error_logging(self):
    ''' not the copy and paste code; enough to give you an idea '''
    http_error = urllib2.HTTPError("mock_url", 404, "Not Found", "mock_headers", None)
    with patch('function.that.invokes.error.handler', side_effect=http_error):
        test_client.get("bad url")

CustomError也是我们的自定义异常。

0 个答案:

没有答案