如何在 Django 测试环境中模拟 googleapiclient
How to mock googleapiclient in a Django test environment
我试图在 Django 单元测试中模拟 googleapiclient.discovery,我查看了这个 post 的表现,它展示了如何模拟 googleapiclient.discovery,所以我尝试了这个:
...
@mock.patch("myapp.views.googleapiclient.discovery")
def test_get_instances(self, mock_discovery):
mock_discovery.build.return_value.service.return_value.instances.return_value.aggregatedList.return_value = {# some mock values}
...
这给出了“找不到模块错误”,指出“myapp.views”不是包
有问题的 views.py 位于 Django 应用程序的标准位置:mysite/myapp/views.py
views.py:
...
from googleapiclient import discovery
def get_instances(proj_id, servicekey)
service = discovery.build('compute','v1', credentials=generate_credentials(servicekey))
request = service.instances().aggregatedList(project=project_id)
...
我是模拟的新手,所以热烈欢迎任何建议
我本来是要改变的
from googleapiclient import discovery
至
import googleapiclient.discovery
我试图在 Django 单元测试中模拟 googleapiclient.discovery,我查看了这个 post 的表现,它展示了如何模拟 googleapiclient.discovery,所以我尝试了这个:
...
@mock.patch("myapp.views.googleapiclient.discovery")
def test_get_instances(self, mock_discovery):
mock_discovery.build.return_value.service.return_value.instances.return_value.aggregatedList.return_value = {# some mock values}
...
这给出了“找不到模块错误”,指出“myapp.views”不是包
有问题的 views.py 位于 Django 应用程序的标准位置:mysite/myapp/views.py
views.py:
...
from googleapiclient import discovery
def get_instances(proj_id, servicekey)
service = discovery.build('compute','v1', credentials=generate_credentials(servicekey))
request = service.instances().aggregatedList(project=project_id)
...
我是模拟的新手,所以热烈欢迎任何建议
我本来是要改变的
from googleapiclient import discovery
至
import googleapiclient.discovery