【发布时间】:2014-10-12 10:51:59
【问题描述】:
我正在为具有一对多关系的 SQLAlchemy 模型类编写单元测试,但无法将模拟对象添加到集合中。
正在测试的类:
class PCLRun(Base):
__tablename__ = 'pcl_runs'
id = Column(Integer, primary_key=True)
...
files = relationship("PCLOutputFile", backref='pcl_run')
class PCLOutputFile(Base):
__tablename__ = 'pcl_output_files'
id = Column(Integer, primary_key=True)
...
pcl_run_id = Column(Integer, ForeignKey('pcl_runs.id'))
测试代码:
class PCLRunTests(unittest.TestCase):
def test_foo(self):
file_mock = mock.Mock()
pcl_run = PCLRun()
pcl_run.files.append(file_mock)
...
附加模拟对象引发异常:
TypeError: 'Mock' object has no attribute '__getitem__'
有没有办法通过向其中添加模拟来对包含关系的类进行单元测试,同时保持集合的行为类似于一个简单的列表?
我使用的是 mock 1.0.1 和 sqlalchemy 0.8.2。
【问题讨论】:
标签: python unit-testing sqlalchemy mocking python-mock