【发布时间】:2019-12-13 21:50:48
【问题描述】:
我想模拟PyMongo提供的aggregate函数用于以下代码:
client = MongoClient(host="localhost", port=27017,username="Harsha", password="Harsha", authSource="admin")
db_obj = client["DB name"]
mongo_result = db_obj[collection_name].aggregate(pipeline)
我想模拟 aggregate 函数。
谁能帮我模拟一下聚合函数?
我尝试了以下代码 sn-p 来模拟聚合函数:
试用 1:
from pymongo import collection
collection_obj = collection.Collection(client["DB name"], "collection_name")
def mock_get(self, *args):
return "Result I want"
@mock.patch(collection_obj.Collection.aggregate, side_effect=mock_get)
def test_demo(self):
.
.
.
.
这不起作用,因为 @mock.patch 需要字符串完整路径参数。
所以我也尝试给出aggregation函数的完整路径
试用 2:
class BasicTest(unittest.TestCase):
def mock_get(self, *args):
return "Result I want"
@mock.patch('pymongo.collection.Collection.aggregate', side_effect=mock_get)
def test_demo(self):
.
.
.
.
这是给我的:
TypeError: test_demo() takes 1 positional argument but 2 were given
【问题讨论】:
标签: python unit-testing mocking pymongo