我需要测试一种可以移动文件的方法。我不希望文件移动操作实际发生,我只需要知道被测方法正确调用shutil.move
什么是最好的修补方法,shutil.move
以便该方法可以在不执行实际文件操作的情况下调用它?
我是这样做的,但是很丑,我想使用模拟库来做到这一点:
real_move = ftp2email.shutil.move
move_operations = []
def fake_move(src, dst):
move_operations.append((src, dst))
ftp2email.shutil.move = fake_move
msg_id = '/path/to/message.xml'
self.ch.mark_message(msg_id)
self.assertEqual(move_operations,
[('/path/to/message.xml', '/path/to/archived/message.xml')])
ftp2email.shutil.move = real_move