我想在我的单元测试中模拟 requests.Response() 对象,我从下面的链接中得到了一个提示。
如何在python中将数据模拟为request.Response类型
在这里,我可以设置status_code值(不是@Property),我想设置@Property 的值text或content
类 UsernamePasswordAuthStrategyTest(TestCase):
def test_do_success(self):
content = self._factory.get_reader(ReaderType.CONTENT).read('MY_TEST')
auth_strategy = UsernamePasswordAuthStrategy(content)
# mock send_request method response
response = Response()
response.status_code = 200
# How could I achieve below line?
response.text = """<html>
<body>
<form method="post" name="NavForm">
<input id="csrfKey" name="csrfKey" type="hidden" value="JEK7schtDx5IVcH1eOWKN9lFH7ptcwHD/"/>
</form>
</body>
</html>"""
auth_strategy.send_request = mock.MagicMock(return_value=response)
session, auth_result = auth_strategy.do() # {'for_next_url_params': {'csrfKey': 'T4WNcz+hXqrxVa5R9o2HXkDm8pNZEi4k/'}}
self.assertTrue(session, 'Test Failed! Something went wrong')
self.assertTrue('for_next_url_params' in auth_result and 'csrfKey' in auth_result['for_next_url_params'],
'Test Failed! …Run Code Online (Sandbox Code Playgroud)