[英]How to Mock __init__ and resolvers with various arguments
I've seen a lot of examples of mock tests but none that show how one could mock something like <graphql.execution.base.ResolveInfo object at 0x106f002a8>
我见过很多模拟测试的例子,但没有一个显示如何模拟像
<graphql.execution.base.ResolveInfo object at 0x106f002a8>
这样的东西
For instance, if I wanted to test that the two methods in this class were working properly, how would I mock the values that are being passed in?例如,如果我想测试这个 class 中的两个方法是否正常工作,我将如何模拟传入的值?
class mySearch(graphene.ObjectType):
my_search = graphene.Field(
MySearchWrapper,
query = graphene.String(description="Search query")
)
def __init__(self, args, context, info):
super(mySearch, self).__init__()
def resolve_my_search(self, args, context, info):
return promisify(MySearchWrapper, args, context, info)
The __init__
method returns: __init__
方法返回:
args: {}, context: <Request 'http://localhost:8080/graphql' [POST]>, info: <graphql.execution.base.ResolveInfo object at 0x106f000c8>
And the resolve_my_search
method returns: resolve_my_search
方法返回:
args: {'page_type': [u'MyCorgi', u'YourCorgi'], 'query': u'Corgi family', 'domain': u'corgidata.com'}, context: <Request 'http://localhost:8080/graphql' [POST]>, info: <graphql.execution.base.ResolveInfo object at 0x106f002a8>
I know I can mock the dictionary value with mock_args.json.return_value
but... not sure about the Request and object. Any ideas?我知道我可以使用
mock_args.json.return_value
来模拟字典值,但是...不确定请求和 object。有什么想法吗? Guidance?指导? I've already spent a week on this and have found no way out.
我已经在这上面花了一个星期,但没有找到出路。
Use unittest.mock python package. 使用unittest.mock python软件包。 Here is an example:
这是一个例子:
from unittest import mock
info = mock.create_autospec(graphql.execution.base.ResolveInfo)
info.context.user = AnonymousUser()
Here is how I did it.我是这样做的。 You can tweak it to mock the
GraphQLResolveInfo
but for this use case I only needed to mock the Request
object.您可以调整它以模拟
GraphQLResolveInfo
,但对于这个用例,我只需要模拟Request
object。
from requests import Requests
from unittest.mock import patch
from starlette.authentication import AuthenticationError
@patch("grapql.type.definition.GraphQLResolveInfo.context")
def test_func(context_mock):
with pytest.raises(AuthenticationError):
context_mock.side_effect = Request()
func_to_be_tested(resolver("fake_source", info_mock, data_inputs))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.