问题
I'm trying to test that my authentication fails. The exception is raised but not caught by assertRaises
. What am I missing here?
def test_auth(self):
from graphql_jwt.exceptions import PermissionDenied
with self.assertRaises(PermissionDenied):
response = self.client.execute(self.query)
Traceback:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
Traceback (most recent call last):
File "/home/dan/game/venv/lib/python3.7/site-packages/promise/promise.py", line 487, in _resolve_from_executor
executor(resolve, reject)
File "/home/dan/game/venv/lib/python3.7/site-packages/promise/promise.py", line 754, in executor
return resolve(f(*args, **kwargs))
File "/home/dan/game/venv/lib/python3.7/site-packages/graphql/execution/middleware.py", line 75, in make_it_promise
return next(*args, **kwargs)
File "/home/dan/game/venv/lib/python3.7/site-packages/graphene_django/filter/fields.py", line 106, in connection_resolver
**args
File "/home/dan/game/venv/lib/python3.7/site-packages/graphene_django/fields.py", line 156, in connection_resolver
iterable = resolver(root, info, **args)
File "/home/dan/game/venv/lib/python3.7/site-packages/graphql_jwt/decorators.py", line 31, in wrapper
return func(info.context, *args, **kwargs)
File "/home/dan/game/venv/lib/python3.7/site-packages/graphql_jwt/decorators.py", line 43, in wrapper
raise exceptions.PermissionDenied()
graphql.error.located_error.GraphQLLocatedError: You do not have permission to perform this action
F.
======================================================================
FAIL: test_auth (api.tests.test_mutations.TestGame)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/dan/game/api/tests/test_mutations.py", line 57, in test_auth
response = self.client.execute(self.query)
AssertionError: PermissionDenied not raised
The exception is being raised here.
回答1:
Your test is not catching a PermissionDenied
exception because something in the code you're running is wrapping that exception in an instance of graphql.error.located_error.GraphQLLocatedError
. Because you're checking for the wrong exception type, the test fails.
I don't know too much about the libraries you're using, and the invisible change of the exception type seems like a horrible mis-feature (it should at least add the code that changes the exception type to the exception traceback so you can debug it). But you may be able to work around the issue, by catching the wrapped exception and rethrowing the original:
def test_auth(self):
from graphql_jwt.exceptions import PermissionDenied
from graphql.error.located_error import GraphQLLocatedError
with self.assertRaises(PermissionDenied):
try:
response = self.client.execute(self.query)
except GraphQLLocatedError as e:
raise e.original_error
来源:https://stackoverflow.com/questions/56679966/exception-raised-but-not-caught-by-assertraises