测试使用继承的参数化构造函数
我想测试如下所示的函数
async def post_lookup(self, request: web.Request):
response = await ZipcodeLookupModel(
request.app[constants.CONNECTOR], session_store=request._state[constants.SESSION_STORAGE]
).lookup(request=request)
return web.json_response(response)
类 ZipcodeLookupModel
及其层次结构如下所示
class ZipcodeLookupModel(BaseModel):
def __init__(self, *args, **kwargs):
super(ZipcodeLookupModel, self).__init__(*args, **kwargs)
class BaseModel:
def __init__(self, connector=None, session_store=None):
self.session_store = session_store
self.connector = connector
self.loop = get_running_loop()
self.service_args = dict(connector=self.connector, loop=self.loop)
我想修补
该对象创建
with patch(
"src.api.v1.zipcode.lookup.model.ZipcodeLookupModel",
new=ZipcodeLookupModelStub()
):
response = await client.session.post(url=f"http://{client.host}:{client.port}{path}", json=self.message)
assert response.status == 200
data = await response.json()
assert data
我已经为测试创建了一个存根对象
class ZipcodeLookupModelStub(BaseModel):
def __init__(self, *args, **kwargs):
super(ZipcodeLookupModelStub, self).__init__(*args, **kwargs)
async def lookup(self, request):
但是当我运行测试时,出现此错误(在 post_lookup
中创建对象的行上),
super() argument 1 must be type, not ZipcodeLookupModelStub
问题是什么?
I want to test the function that looks like this
async def post_lookup(self, request: web.Request):
response = await ZipcodeLookupModel(
request.app[constants.CONNECTOR], session_store=request._state[constants.SESSION_STORAGE]
).lookup(request=request)
return web.json_response(response)
The class ZipcodeLookupModel
and its hierarchy looks like this
class ZipcodeLookupModel(BaseModel):
def __init__(self, *args, **kwargs):
super(ZipcodeLookupModel, self).__init__(*args, **kwargs)
class BaseModel:
def __init__(self, connector=None, session_store=None):
self.session_store = session_store
self.connector = connector
self.loop = get_running_loop()
self.service_args = dict(connector=self.connector, loop=self.loop)
I want to patch
that object creation
with patch(
"src.api.v1.zipcode.lookup.model.ZipcodeLookupModel",
new=ZipcodeLookupModelStub()
):
response = await client.session.post(url=f"http://{client.host}:{client.port}{path}", json=self.message)
assert response.status == 200
data = await response.json()
assert data
I have created a stub object for the test
class ZipcodeLookupModelStub(BaseModel):
def __init__(self, *args, **kwargs):
super(ZipcodeLookupModelStub, self).__init__(*args, **kwargs)
async def lookup(self, request):
But when I run the test I get this error (on the line where the object is created in post_lookup
)
super() argument 1 must be type, not ZipcodeLookupModelStub
What is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要将名称
ZipCodeLookupModel
替换为您要使用的类的实例,而不是类本身。You are replacing the name
ZipCodeLookupModel
with an instance of the class you want to use, not the class itself.