我注意到python中assert_called_once
和assert_called_once_with
的奇怪行为。这是我真正的简单测试:
文件模块/ a.py
from .b import B
class A(object):
def __init__(self):
self.b = B("hi")
def call_b_hello(self):
print(self.b.hello())
文件模块/ b.py
class B(object):
def __init__(self, string):
print("created B")
self.string = string;
def hello(self):
return self.string
这些是我的测试:
import unittest
from mock import patch
from module.a import A
class MCVETests(unittest.TestCase):
@patch('module.a.B')
def testAcallBwithMockPassCorrect(self, b1):
a = A()
b1.assert_called_once_with("hi")
a.call_b_hello()
a.b.hello.assert_called_once()
@patch('module.a.B')
def testAcallBwithMockPassCorrectWith(self, b1):
a = A()
b1.assert_called_once_with("hi")
a.call_b_hello()
a.b.hello.assert_called_once_with()
@patch('module.a.B')
def testAcallBwithMockFailCorrectWith(self, b1):
a = A()
b1.assert_called_once_with("hi")
a.b.hello.assert_called_once_with()
@patch('module.a.B')
def testAcallBwithMockPassWrong(self, b1):
a = A()
b1.assert_called_once_with("hi")
a.b.hello.assert_called_once()
if __name__ == '__main__':
unittest.main()
我在函数名称中陈述的问题是:
我做错了吗?我不确定但是阅读文档docs python:
assert_called_once(* args,** kwargs)
断言模拟只被调用一次。
答案 0 :(得分:0)
这是旧的,但对于其他登陆这里的人...
assert_Called_once不是问题,因此您实际上是在进行模拟的函数调用,而不会出错
请参阅:http://engineroom.trackmaven.com/blog/mocking-mistakes/
您可以改为查看通话计数