我正在为基于龙卷风的 Web 应用程序编写一个测试模块。该应用程序使用电机作为 mongodb 连接器,我希望我的测试在临时数据库上运行。我在连接器客户端的delegate_class上使用了一种模拟技术,如下所示:
import json
import mock
import motor
import tornado.ioloop
import tornado.testing
import mongomock
import myapp
patch_motor_client = mock.patch('motor.motor_tornado.MotorClient.__delegate_class__', new=mongomock.MongoClient)
patch_motor_database = mock.patch('motor.motor_tornado.MotorDatabase.__delegate_class__', new=mock.MagicMock)
patch_motor_client.start()
patch_motor_database.start()
class TestHandlerBase(tornado.testing.AsyncHTTPTestCase):
"""
Base test handler
"""
def setUp(self):
# Create your Application for testing
self.application = myapp.app.Application()
super(TestHandlerBase, self).setUp()
def get_app(self):
return self.application
def get_new_ioloop(self):
return tornado.ioloop.IOLoop.instance()
class TestMyHandler(TestHandlerBase):
def test_post_ok(self):
"""
POST a resource is OK
"""
post_args = {
'data': 'some data here..'
}
response = self.fetch('myapi/v1/scripts', method='POST', body=json.dumps(post_args))
# assert status is 201
self.assertEqual(response.code, 201)
当我启动我的测试时,我收到了这个错误:
File "/data/.virtualenvs/myapp/lib/python3.5/site-packages/motor/core.py", line 162, in __getitem__
return db_class(self, name)
File "/data/.virtualenvs/myapp/lib/python3.5/site-packages/motor/core.py", line 217, in __init__
client.delegate, name, **kwargs)
File "/data/.virtualenvs/myapp/lib/python3.5/site-packages/pymongo/database.py", line 102, in __init__
read_concern or client.read_concern)
File "/data/.virtualenvs/myapp/lib/python3.5/site-packages/pymongo/common.py", line 614, in __init__
raise TypeError("codec_options must be an instance of "
TypeError: codec_options must be an instance of bson.codec_options.CodecOptions
目前我无法让它工作,我想知道我想要做的事情是否可以使用当前版本的电机(1.2.1),mongomock(3.8.0)和龙卷风(4.5)。 3),或者我错过了什么?
感谢您的所有建议。