I'm writing tests for my API client. I need to mock the get
function so that it won't make any requests. So instead of returning a Response
object I want to return a MagicMock
. But then pydantic raises ValidationError
because it is going to the model.
I have the following pydantic model:
class Meta(BaseModel): raw: Optional[str] response: Optional[Response] class Config: arbitrary_types_allowed = True
which raises:
> ???E pydantic.error_wrappers.ValidationError: 1 validation error for OneCallResponseE meta -> responseE instance of Response expected (type=type_error.arbitrary_type; expected_arbitrary_type=Response)
The one solution would be to add Union
with MagicMock
but I really don't want to change the code for tests. That is not the way.
class Meta(BaseModel): raw: Optional[str] response: Optional[Union[Response, MagicMock]] class Config: arbitrary_types_allowed = True
Any ideas how to patch/mock it?