简体   繁体   English

模拟请求发布响应 __dict__ 和 status_code python

[英]mock requests post responses __dict__ and status_code python

I need to mock a request.post with both status_code and __dict__ .我需要用status_code__dict__模拟 request.post 。 I tried this我试过这个


    @patch("tests.requests.post", Autospec=True)
    def test_create_job(self,, mock_post):
        mock_post.return_value = Mock(status_code=200, __dict__={"a": "b"})

without success, I tried this one also mock_post.return_value.__dict__ = {'a': 'b'} and works but not when I want also the 200没有成功,我也尝试了这个mock_post.return_value.__dict__ = {'a': 'b'}并且工作但不是当我也想要 200

any suggestion?有什么建议吗?

You can do:你可以做:

mock_post.return_value.__dict__ = {'a': 'b', status_code=200}

But this way you replace the dictionary with all of Mock attributes, after which you won't be able to call for example assert_called_once_with method.但是通过这种方式,您可以用所有Mock属性替换字典,之后您将无法调用例如assert_called_once_with方法。

Why do are you trying to set custom __dict__ ?你为什么要设置自定义__dict__ Why not:为什么不:

mock_post.return_value = 200
mock_post.a = 'b'

or或者

mock_post.return_value = Mock(return_value=200, a=b)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM