Tôi đã bỏ tất cả các phương pháp từ mô-đun API bình tĩnh của tôi. Bây giờ tôi muốn thử nghiệm phương thức get bằng cách thực sự thực hiện cuộc gọi API. Tôi mong đợi lỗi 400 từ thử nghiệm này.Nhận 500 TRỰC TUYẾN MÁY L INTERNI NỘI BỘ khi bỏ một (GET) bình thường Gọi API GET
lớp tài nguyên của tôi
class Response(Resource):
@marshal_with(response_params_get_responses_on_job)
def get(self, filter_name=None):
try:
response = self.process_get_request(filter_name)
if not response['users']:
raise MyValidationError("No data found")
return response
except MyValidationError as err:
abort(404, message=err)
except ValueError as mistake:
abort(400, message=mistake)
My UnitTest
# TODO - Failing!
@mock.patch('application.resources.response.Response.process_get_request', autospec=True)
def test_get_400(self, process_get_request_mock):
process_get_request_mock.side_effect = ValueError("some error")
app = Flask(__name__)
app.debug = True
api = Api(app, prefix='/api/v2')
api.add_resource(Response, '/user/responses', endpoint='job_responses')
api.init_app(app)
with app.test_client() as client:
resp = client.get('/api/v2/user/responses',
environ_base={'HTTP_USER_AGENT': 'Chrome'},
headers={'Content-type': 'application/json'})
self.assertEqual(resp.status_code, 400)
thử nghiệm của tôi thất bại vì phản ứng tôi nhận được là lỗi 500
AssertionError: 500 != 400
Stac ktrace
Failure
Traceback (most recent call last):
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/mock.py", line 1201, in patched
return func(*args, **keywargs)
File "/home/hussain/workspace/my-app/tests/unittests/test_Response.py", line 38, in test_get_400
headers={'Content-type': 'application/json'})
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/werkzeug/test.py", line 774, in get
return self.open(*args, **kw)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask/testing.py", line 108, in open
follow_redirects=follow_redirects)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/werkzeug/test.py", line 742, in open
response = self.run_wsgi_app(environ, buffered=buffered)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/werkzeug/test.py", line 659, in run_wsgi_app
rv = run_wsgi_app(self.application, environ, buffered=buffered)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/werkzeug/test.py", line 867, in run_wsgi_app
app_rv = app(environ, start_response)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 270, in error_router
return original_handler(e)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 270, in error_router
return original_handler(e)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 267, in error_router
return self.handle_error(e)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 267, in error_router
return self.handle_error(e)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 270, in error_router
return original_handler(e)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 270, in error_router
return original_handler(e)
File "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask/app.py", line 1363, in handle_user_exception
assert exc_value is e
AssertionError
Bằng cách đặt một điểm debug trong get
phương pháp, tôi thấy rằng việc thực hiện đi đến abort(400, message=mistake)
.
Sau đó, điều gì đang xảy ra? Tại sao thử nghiệm của tôi thất bại?
Tôi không phải là chuyên gia về Flask nhưng AFAIK bạn sử dụng cú pháp 'abort()' sai. Nếu bạn in nội dung 'resp' có thể bạn sẽ tìm thấy một' TypeError' đăng nhập. Hãy xem http://stackoverflow.com/a/21301229/4101725 nếu bạn muốn biết cách sử dụng nó. Nhưng ảnh đầu tiên có thể được sử dụng chỉ là 'abort (400)' hoặc 'abort (404)'. Dù sao bạn nên hạnh phúc: thử nghiệm của bạn tìm thấy một lỗi ngấm ngầm trong xử lý lỗi :) –
... chỉ một điều nữa ... Hãy cho tôi biết nếu nó hoạt động mà tôi sẽ nộp một câu trả lời. –
Xin chào. Bạn đúng rồi! Không có vấn đề trong cú pháp hủy bỏ, Đó là những gì tôi đã đi qua nó. 'Message' kwarg mà tôi chuyển tới' abort' phải là một chuỗi. Vì vậy, tôi đã thay đổi nó thành 'hủy bỏ (404, message = err.message)'. Tôi cần ghi nhớ điều này. Cảm ơn đã giúp đỡ. – Hussain