我正在尝试使用Python中的mock库进行一些单元测试。我有以下代码:
def a():
print 'a'
def b():
print 'b'
if some condition
a()
当对b
进行模拟调用时,如何断言已对b
进行了调用?我尝试了以下代码,但失败了:
mymock=Mock()
mymock.b()
assertTrue(a.__call__ in mymock.mock_calls)
出于某种原因,我认为mymock.b()
与方法b()
无关。对此能做些什么?
Tags:
如果对
a
进行修补,则可以确保按如下方式调用它:如果方法位于其他模块中:
你说得对。当你模仿一个对象时,你是在暗示你不在乎你的模仿在幕后做什么。如果要确保从
b
调用a
,则需要在b
中patcha
。所以,这个故事的寓意是,模仿或修补你想测量的对象而不是你关心的对象实现。在本例中,您关心
b
,并希望了解它如何使用a
。既然我们不在乎a
做什么,只要它被调用,我们就可以修补它。此外,如果您希望获得有关对a的调用的更多详细信息,而不是
assert_called_with
,则可以通过访问patchesmock_calls
属性来分析所有调用。在这种情况下应该是patch_a.mock_calls
。相关问题 更多 >
编程相关推荐