Python 未使用mock.patch调用函数

Python 未使用mock.patch调用函数,python,unit-testing,mocking,Python,Unit Testing,Mocking,我正在尝试测试连接到BluetoothSocket的BluetoothClient类。为了避免使用真正的套接字,我只想测试使用正确的参数调用套接字中的connect()方法。使用mock.patch替换bluetooth_控制模块中导入的bluetooth模块的效果与预期不符 在我看来,调用了connect()方法,但断言告诉我不是这样 代码: 正在测试的单元(bluetooth_control.py): 测试(bluetooth\u control\u Test.py): 输出: 设置 连接:

我正在尝试测试连接到BluetoothSocket的BluetoothClient类。为了避免使用真正的套接字,我只想测试使用正确的参数调用套接字中的connect()方法。使用mock.patch替换bluetooth_控制模块中导入的bluetooth模块的效果与预期不符

在我看来,调用了connect()方法,但断言告诉我不是这样

代码: 正在测试的单元(bluetooth_control.py): 测试(bluetooth\u control\u Test.py): 输出:
设置
连接:sock=
测试仪建立连接插座=
FtearDown
======================================================================
失败:testEstablishConnection(\uuuuu main\uuuuuu.TestShelf)
----------------------------------------------------------------------
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.7/site packages/mock.py”,第1201行,带补丁
返回函数(*参数,**键盘)
testEstablishConnection中第21行的文件“bluetooth\u control\u test.py”
mock_bluetooth.connect()
文件“/usr/lib/python2.7/site packages/mock.py”,第831行,在assert_中使用
raise AssertionError('预期调用:%s\n未调用“%”(预期,))
AssertionError:预期调用:mock(,('98:D3:31:B2:EF:32',1))
不打电话
----------------------------------------------------------------------
在0.003s内运行1次测试
失败(失败=1)

两天后,我再次检查了这个问题,发现了我犯的愚蠢错误。我必须修补实际的方法,并删除断言中错误添加的括号

我不打算删除这个问题,这样也许可以帮助别人避免这些错误

试验
import bluetooth

class BluetoothClient(object):
    def __init__(self):
        self.address="98:D3:31:B2:EF:32"
        self.port=1

    def establishConnection(self):
        self.createSocket()
        self.connect()

    def createSocket(self):
        self.sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )

    def connect(self):
        print "connect: sock="+str(self.sock)
        self.sock.connect((self.address, self.port))
import unittest
import mock
import bluetooth_control
import bluetooth

class TestShelf(unittest.TestCase):

    def setUp(self):
        unittest.TestCase.setUp(self)
        self.bc = bluetooth_control.BluetoothClient()
        print "setUp"

    def tearDown(self):
        self.shelf = None
        print "tearDown"

    @mock.patch('bluetooth_control.bluetooth')
    def testEstablishConnection(self,mock_bluetooth):
        self.bc.establishConnection()
        print "testEstablishConnection sock="+str(self.bc.sock)
        mock_bluetooth.connect().assert_called_with(self.bc.sock,("98:D3:31:B2:EF:32",1))


if __name__ == "__main__":
    unittest.main()
setUp
connect: sock=<MagicMock name='bluetooth.BluetoothSocket()' id='140433322111504'>
testEstablishConnection sock=<MagicMock name='bluetooth.BluetoothSocket()' id='140433322111504'>
FtearDown

======================================================================
FAIL: testEstablishConnection (__main__.TestShelf)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/mock.py", line 1201, in patched
    return func(*args, **keywargs)
  File "bluetooth_control_test.py", line 21, in testEstablishConnection
    mock_bluetooth.connect().assert_called_with(self.bc.sock,("98:D3:31:B2:EF:32",1))
  File "/usr/lib/python2.7/site-packages/mock.py", line 831, in assert_called_with
    raise AssertionError('Expected call: %s\nNot called' % (expected,))
AssertionError: Expected call: mock(<MagicMock name='bluetooth.BluetoothSocket()' id='140433322111504'>, ('98:D3:31:B2:EF:32', 1))
Not called

----------------------------------------------------------------------
Ran 1 test in 0.003s

FAILED (failures=1)
import unittest
import mock
import bluetooth_control
import bluetooth

class TestShelf(unittest.TestCase):

    def setUp(self):
        unittest.TestCase.setUp(self)
        self.bc = bluetooth_control.BluetoothClient()
        print "setUp"

    def tearDown(self):
        self.shelf = None
        print "tearDown"

    @mock.patch('bluetooth_control.bluetooth.BluetoothSocket.connect')
    def testEstablishConnection(self,mock_connect):
        self.bc.establishConnection()
        print "testEstablishConnection sock="+str(self.bc.sock)
        mock_connect.assert_called_with(("98:D3:31:B2:EF:32",1))


if __name__ == "__main__":
    unittest.main()