简体   繁体   English

引发异常但未被 assertRaises 捕获

[英]Exception raised but not caught by assertRaises

I'm trying to test that my authentication fails.我正在尝试测试我的身份验证是否失败。 The exception is raised but not caught by assertRaises .异常被引发但未被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 . 这里提出例外。

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 .您的测试未捕获PermissionDenied异常,因为您正在运行的代码中的某些内容将该异常包装在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

GraphQL, by definition catches all exceptions and puts errors in the errors part of the response.根据定义,GraphQL 会捕获所有异常并将错误放入响应的错误部分。 If you're testing the execution of a query (self.client.execute(... query ...)) you should get the result and and verify it has an errors part that matches what you expect.如果您正在测试查询 (self.client.execute(... query ...)) 的执行,您应该得到结果并验证它有一个与您期望的匹配的错误部分。

An easier way would be to test the resolver specifically - call resolve_entity directly and not via the GraphQL execution layer, and test it like you would any other Python function.一种更简单的方法是专门测试解析器 - 直接调用 resolve_entity 而不是通过 GraphQL 执行层,并像测试任何其他 Python 函数一样对其进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM