Mocking Grpc Status Code ('rpcerror' Object Has No Attribute 'code') In Flask App
I have to create a unittest that should mock a specific grpc status code (in my case I need NOT_FOUND status). This is what i want to mock: try: # my mocked function except g
Solution 1:
Looks like you cannot construct a valid RpcError
object from Python the same way it is created in the grpc
code (which is written in C). You can, however, emulate the behavior by using:
defmock_function_which_raise_RpcError():
e = grpc.RpcError()
e.code = lambda: grpc.StatusCode.NOT_FOUND
raise e
Alternatively, you can derive your own exception:
classMyRpcError(grpc.RpcError):def__init__(self, code):
self._code = code
defcode(self):
returnself._code
defmock_function_which_raise_RpcError():
raise MyRpcError(grpc.StatusCode.NOT_FOUND)
Update:
Added missing code
method as mentioned by Martin Grůber in the comments.
Post a Comment for "Mocking Grpc Status Code ('rpcerror' Object Has No Attribute 'code') In Flask App"