所以我试图运行一个带有两个修补模拟的单元,其中第一个修补模拟(insertIntoParent
)被调用三次并返回[17,18,19]
的值。这是我的测试:
@mock.patch('dao.AccessSQLInterface.AccessSQLInterface.insertIntoParent')
@mock.patch('dao.AccessSQLInterface.AccessSQLInterface.insertBulkData')
def test_hiertablecreatebulkinsert(self, insertBulkData, insertIntoParent):
parse = ParseWorksheet()
dao = AccessSQLInterface()
dao.insertIntoParent = MagicMock(side_effect=[17,18,19])
wb = openpyxl.Workbook()
ws = wb.create_sheet('SheetData')
dictHier = {}
dictHier['C1'] = 4
dictHier['C2'] = 2
ws['A1'].value = 'Nameheader'
ws['C1'].value = 'Parent1'
ws['C2'].value = 'Parent2_1'
ws['E2'].value = 'Parent2_2'
ws['A4'].value = 'Cat1'
ws['A5'].value = 'Cat2'
ws['C4'].value = 'TxtVal1'
ws['D5'].value = 4
ws['E5'].value = 'TxtVal6'
ws['F4'].value = 7
genHeader = 'INSERT INTO dbo.Tbl1(Nameheader,Header1,Header2,ParentXID,StudyXID)'
valueString1 = "('Cat1','TxtVal1',NULL,18,1),('Cat2',NULL,4,18,1)"
valueString2 = "('Cat1',NULL,7,19,1),('Cat2','TxtVal6',NULL,19,1)"
parse.createHierTableInserts(1, ws, 'Tbl1', genHeader, dictHier, 'A', 'False','C4','F5',dao)
insertIntoParent.assert_called_once()
insertBulkData.assert_any_call(genHeader, valueString1)
insertBulkData.assert_any_call(genHeader, valueString2)
我知道这一行:( dao.insertIntoParent = MagicMock(side_effect=[17,18,19])
)正确地模拟了回复,因为副作用值显示在insertBulkData.assert_any_call(genHeader, valueString[])
检查中。 insertIntoParent.assert_called_once()
行上的测试失败,说'insertIntoParent被调用了0次',但是如果我把它注释掉,测试就会通过。 dao.insertIntoParent
是否会覆盖我的insertIntoParent
补丁模拟?我该如何设置?
答案 0 :(得分:0)
所以实际上这是一个非常简单的修复,我只需要更改副作用线(正如我想的那样,它覆盖了我的修补模拟)。这是我的新测试:
@mock.patch('dao.AccessSQLInterface.AccessSQLInterface.insertIntoParent')
@mock.patch('dao.AccessSQLInterface.AccessSQLInterface.insertBulkData')
def test_hiertablecreatebulkinsert(self, insertBulkData, insertIntoParent):
parse = ParseWorksheet()
dao = AccessSQLInterface()
insertIntoParent.side_effect=[1,2,3]
wb = openpyxl.Workbook()
ws = wb.create_sheet('SheetData')
dictHier = {}
dictHier['C1'] = 4
dictHier['C2'] = 2
ws['A1'].value = 'Nameheader'
ws['C1'].value = 'Parent1'
ws['C2'].value = 'Parent2_1'
ws['E2'].value = 'Parent2_2'
ws['A4'].value = 'Cat1'
ws['A5'].value = 'Cat2'
ws['C4'].value = 'TxtVal1'
ws['D5'].value = 4
ws['E5'].value = 'TxtVal6'
ws['F4'].value = 7
genHeader = 'INSERT INTO dbo.Tbl1(Nameheader,Header1,Header2,ParentXID,StudyXID)'
valueString1 = "('Cat1','TxtVal1',NULL,2,1),('Cat2',NULL,4,2,1)"
valueString2 = "('Cat1',NULL,7,3,1),('Cat2','TxtVal6',NULL,3,1)"
parse.createHierTableInserts(1, ws, 'Tbl1', genHeader, dictHier, 'A', 'False','C4','F5',dao)
insertIntoParent.assert_any_call('Parent1',mock.ANY,'C1','Tbl1','NULL')
insertIntoParent.assert_any_call('Parent2_1',mock.ANY,'C2','Tbl1',1)
insertIntoParent.assert_any_call('Parent2_2',mock.ANY,'E2','Tbl1',1)
insertBulkData.assert_any_call(genHeader, valueString1)
insertBulkData.assert_any_call(genHeader, valueString2)