我有一个函数,想写一个unittest来检查我的代码是否使用模拟库捕获ConnectionError
def get_foo():
try:
return requests.get("http://www.bongani.com")
except requests.exceptions.ConnectionError:
print("Error")
我有什么:
import unittest
import mock
class MyTestCase(unittest.TestCase):
@mock.patch('requests.get')
def test_foo(self, mock_requests_get):
mock_requests_get.side_effect = requests.exceptions.ConnectionError()
with self.assertRaises(requests.exceptions.ConnectionError):
get_foo()
if __name__ == '__main__':
unittest.main()
我收到此错误:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/mock/mock.py", line 1305 , in patched
return func(*args, **keywargs)
File "<ipython-input-24-15266e4f708a>", , in test_foo
get_foo()
AssertionError: ConnectionError not raised
我想模拟return requests.get("http://www.bongani.com")
行,以便在调用
答案 0 :(得分:0)
确保修补正确的路径
yarn why node_modules/*
您不想实例化异常。
yarn why