我试图为以下方法编写一些测试用例,作为名为Project的模型的一部分:
def get_mouse_model_designs(self):
return {details.design.to_mouse_model()
for details in self.strategies.all()}
问题似乎出在details.design.to_mouse_model()
中,我似乎无法准确地模拟这个函数。这是我的测试(self.details2
是这个测试用例中链接到项目的唯一模型,因此它将是self.strategies.all()
返回的唯一记录):
def test_mouse_model_designs_one_design(self):
mm_design = MagicMock()
self.details2.design.to_mouse_model = MagicMock(return_value=mm_design)
self.assertEqual(self.project2.get_mouse_model_designs(), {mm_design})
下面是我收到的错误消息:
AssertionError: Items in the first set but not the second:
<MouseModel.LabWork.DesignTask.DesignTask object at 0x0A4B0910>
Items in the second set but not the first:
<MagicMock id='172651760'>
MouseModel.LabWork.DesignTask.DesignTask对象是to_mouse_model()
方法返回的对象。但我嘲笑了这一点。因此,从错误消息中我可以看出,它实际上并不是在模仿to_mouse_model()
方法。我试图断言该方法已被调用,但也失败了
但是,如果我删除函数定义中的to_mouse_model()
,并相应地更新测试,它就会通过
任何帮助都将不胜感激
我找到了解决办法。通过模拟类方法而不是对象方法:
相关问题 更多 >
编程相关推荐