簡體   English   中英

mongomock如何與馬達一起使用?

[英]How can mongomock can be use with motor?

我有一個用Tornado和Motor實現的服務器,我遇到過這個pymongo的模擬: https//github.com/vmalloc/mongomock

為了快速運行它,我真的很喜歡對我的代碼進行單元測試而沒有真正調用DB的想法。

我已經嘗試修補馬達來將調用傳遞給mongomock,就像那樣:

from mock import MagicMock
import mongomock
p = mock.patch('motor.MotorClient.__delegate_class__', new=mongomock.MongoClient)
p1 = mock.patch('motor.MotorDatabase.__delegate_class__', new=MagicMock())

p.start()
p1.start()

def fin():
    p.stop()
    p1.stop()

request.addfinalizer(fin)

它失敗了:

Traceback (most recent call last):
  File "C:\Users\ifruchte\venv\lib\site-packages\pytest_tornado\plugin.py", line 136, in http_server
    http_app = request.getfuncargvalue(request.config.option.app_fixture)
  File "C:\Users\ifruchte\venv\lib\site-packages\_pytest\python.py", line 1337, in getfuncargvalue
    return self._get_active_fixturedef(argname).cached_result[0]
  File "C:\Users\ifruchte\venv\lib\site-packages\_pytest\python.py", line 1351, in _get_active_fixturedef
    result = self._getfuncargvalue(fixturedef)
  File "C:\Users\ifruchte\venv\lib\site-packages\_pytest\python.py", line 1403, in _getfuncargvalue
    val = fixturedef.execute(request=subrequest)
  File "C:\Users\ifruchte\venv\lib\site-packages\_pytest\python.py", line 1858, in execute
    self.yieldctx)
  File "C:\Users\ifruchte\venv\lib\site-packages\_pytest\python.py", line 1784, in call_fixture_func
    res = fixturefunc(**kwargs)
  File "C:\Users\ifruchte\PycharmProjects\pyrecman\tests\__init__.py", line 65, in app
    return get_app(db=motor_db(io_loop))
  File "C:\Users\ifruchte\PycharmProjects\pyrecman\tests\__init__.py", line 27, in motor_db
    return motor.MotorClient(options.mongo_url, io_loop=io_loop)[options.db_name]
  File "C:\Users\ifruchte\venv\lib\site-packages\motor\__init__.py", line 1003, in __getattr__
    return MotorDatabase(self, name)
  File "C:\Users\ifruchte\venv\lib\site-packages\motor\__init__.py", line 1254, in __init__
    delegate = Database(connection.delegate, name)
  File "C:\Users\ifruchte\venv\lib\site-packages\pymongo\database.py", line 61, in __init__
    **connection.write_concern)
TypeError: attribute of type 'Collection' is not callable

誰知道怎么做? 或者我在這里浪費時間?

無需實例化magicmock。

p1 = mock.patch('motor.MotorDatabase.__delegate_class__', new=MagicMock)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM