python mock.patch()不模拟导入的类模块
我有一些看起来像这样的代码(删节):
## ./module_1/lambda_function.py
from shared.graphql_helpers import GraphQLClient
gql_client = GraphQLClient('http://host_url.test/')
post_event_to_consumer(event):
response = gql_client.make_query(mutation, { 'data': event, {})
lambda_hanbdler(event, context):
post_event_to_consumer(event['detail']
导入的模块是这样的:
## ./shared/graphql_helpers.py
import requests
class GraphQLClient:
def __init__(self, url):
self.host_url = url
def make_query(self, query, variables, headers):
request = requests.post(self.host_url, json={'query': query, 'variables': variables}, headers=headers)
if request.status_code != 200:
raise Exception()
elif request.json()['errors']:
raise Exception()
else:
return request.json()['data']
项目结构是这样的:
module_1
__init__.py
lambda_function.py
lambda_function_test.py
shared
__init__.py
graphql_helpers.py
我的问题是我试图在lambda_function in
/code>,但似乎不起作用。graphqlclient
lambda_function_test.py <
我的单元测试看起来像这样:
## ./module_1/lambda_function_test.py
import os
from unittest import mock, TestCase
class TestLambdaFunction(TestCase):
@mock.patch('module_1.lambda_function.GraphQLClient')
def test_post_event_to_compplex(self, mock_gql_client):
mock_gql_client = mock.Mock()
mock_gql_client.make_query.return_value = {}
result = post_event_to_consumer(json_data['detail'])
assert result == None
根据我在线阅读的内容,patch
模拟了SUT的命名空间中的导入,但是,当我运行测试时,它会导入原始的GraphQlClclient并抛出ConnectionError。我还尝试从路径上删除module_1
,但这也不起作用。
不确定这里是什么不正确的,但是任何帮助将不胜感激。
I have a some code that looks like this (abridged):
## ./module_1/lambda_function.py
from shared.graphql_helpers import GraphQLClient
gql_client = GraphQLClient('http://host_url.test/')
post_event_to_consumer(event):
response = gql_client.make_query(mutation, { 'data': event, {})
lambda_hanbdler(event, context):
post_event_to_consumer(event['detail']
The module imported is such:
## ./shared/graphql_helpers.py
import requests
class GraphQLClient:
def __init__(self, url):
self.host_url = url
def make_query(self, query, variables, headers):
request = requests.post(self.host_url, json={'query': query, 'variables': variables}, headers=headers)
if request.status_code != 200:
raise Exception()
elif request.json()['errors']:
raise Exception()
else:
return request.json()['data']
The project structure is such:
module_1
__init__.py
lambda_function.py
lambda_function_test.py
shared
__init__.py
graphql_helpers.py
My problem is that I am trying to patch GraphQLClient
in lambda_function_test.py
but it does not seem to be working.
My unit test looks like this:
## ./module_1/lambda_function_test.py
import os
from unittest import mock, TestCase
class TestLambdaFunction(TestCase):
@mock.patch('module_1.lambda_function.GraphQLClient')
def test_post_event_to_compplex(self, mock_gql_client):
mock_gql_client = mock.Mock()
mock_gql_client.make_query.return_value = {}
result = post_event_to_consumer(json_data['detail'])
assert result == None
From what I read online, patch
mocks the import in the SUT's namespace, however, when I run the test, it imports the original GraphQLClient and throws a ConnectionError. I also tried removing module_1
from the path but that didn't work either.
Not sure what's incorrect here, but any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论