1
試圖讓Configparser.read
使用模擬引發異常。mocking configparser.Configparser()。讀取以返回異常
# foo.py
from six.moves import configparser
def file1_formatted_correctly():
try:
parser = configparser.ConfigParser()
parser.read(os.path.join("user/files/", 'configfile1'))
except configparser.Error: # I want to raise this in the unit test
return False
return True
這是單元測試:
@mock.patch('foo.configparser.ConfigParser')
def test_exception_raises(self, mocked_parser):
mock_parser.read.side_effect = configparser.Error("Error")
# I want to get the method to return True back
# by raising the configparser.Error exception
self.assertTrue(foo.file1_formatted_correctly())
但是,這似乎並沒有真正引發異常。