如何用各种参数模拟__init__和解析器

时间:2017-11-15 00:54:52

标签: python unit-testing mocking

我见过很多模拟测试的例子,但没有一个能说明如何模仿<graphql.execution.base.ResolveInfo object at 0x106f002a8>

例如,如果我想测试这个类中的两个方法是否正常工作,我将如何模拟传入的值?

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)

__init__方法返回:

args: {}, context: <Request 'http://localhost:8080/graphql' [POST]>, info: <graphql.execution.base.ResolveInfo object at 0x106f000c8>

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>

我知道我可以使用mock_args.json.return_value来模拟字典值但是...不确定请求和对象。有任何想法吗?指导?我已经花了一个星期的时间在这上面找不到任何出路。

1 个答案:

答案 0 :(得分:0)

使用unittest.mock python包。这是一个例子:

from unittest import mock

info = mock.create_autospec(graphql.execution.base.ResolveInfo)
info.context.user = AnonymousUser()