Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python mocking requests.get内容和iter_行()_Python_Unit Testing_Mocking_Python Requests - Fatal编程技术网

Python mocking requests.get内容和iter_行()

Python mocking requests.get内容和iter_行(),python,unit-testing,mocking,python-requests,Python,Unit Testing,Mocking,Python Requests,我试图模拟一个在内部使用它的对象的streamed requests.get函数 我特别需要测试以下方法: class MyObject(object) def __iter__(self): payload = {"op": "OPEN"} response = requests.get("http://" + self.parsed_uri.uri_path, params=payload, stream=True) return re

我试图模拟一个在内部使用它的对象的streamed requests.get函数

我特别需要测试以下方法:

class MyObject(object)
    def __iter__(self):
        payload = {"op": "OPEN"}
        response = requests.get("http://" + self.parsed_uri.uri_path, params=payload, stream=True)
        return response.iter_lines()

    def read(self, size=None):
        if not size or size < 0:
            payload = {"op": "OPEN", "offset": self.offset}
            self.offset = 0
        else:
            payload = {"op": "OPEN", "offset": self.offset, "length": size}
            self.offset = self.offset + size
        response = requests.get("http://" + self.parsed_uri.uri_path, params=payload, stream=True)
        return response.content

我正在尝试实现mocked_请求,但不幸的是,我无法让它工作。可以这样使用模拟库吗?模拟请求得到的
应该是什么样子?

UPD。如果没有完整的示例,就无法说出代码的真正错误。我如何使用
响应解决此问题
模块:

import requests
import unittest
import responses


class Foo(object):
    def __init__(self, uri):
        self.uri = uri

    def __iter__(self):
        payload = {"op": "OPEN"}
        response = requests.get("http://" + self.uri, params=payload,
                                stream=True)
        return response.iter_lines()


class TestFoo(unittest.TestCase):
    @responses.activate
    def test_iter_lines(self):
        responses.add(responses.GET, "http://local.host/", body='abc\n123')
        r = iter(Foo("local.host"))
        self.assertEqual(r.next(), "abc")
        self.assertEqual(r.next(), "123")

if __name__ == '__main__':
    unittest.main()
另外,在调用此函数之前,您是否希望返回迭代器

self.assertEqual(object.next(), "123")

它只包含
self.offset
self.parsed_uri
,没有其他内容。你是说
\uuuu init\uuu
?那么
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。现在唯一的问题是,它在Python3
b“abc”
中返回字节字符串,在Python2
“abc”
中返回普通字符串。
self.assertEqual(object.next(), "123")