Provide instructions on how to write decorators like @mock_{resource}
See original GitHub issueI apologize, but this isn’t a bug report or a feature request, and there doesn’t appear to be a discussion group anywhere. If someone can point me to somewhere where it’s more appropriate to ask questions like this, I’ll be happy to oblige.
This is one of the best testing libraries I’ve ever worked with and I want to use its principles to mock other APIs I work with.
My main questions is how the decorators (or context managers) work under the hood. I’ve spent a few hours reading the code, but I’ll admit there’s too much abstraction involved for me to follow some of the details. I’m looking for how I can recreate the terrific convenience of decorators like mock_s3
as opposed to having to directly patch imports for every module in my code which imports them.
e.g., it’s great how in this library you can do this
@mock_s3
def test_some_biz_logic():
biz_logic_caller()
as opposed to having to do
@patch("biz_logic.boto3", mock_boto3):
def test_biz_logic_caller():
biz_logic_caller()
where a @patch
is needed for every module involved where boto3
is imported.
I posted an SO post here https://stackoverflow.com/questions/67138758/use-decorator-to-mock-an-import-within-the-wrapped-function
Am I right that the patching here https://github.com/spulec/moto/blob/master/moto/core/models.py#L508 is somehow globally patching the boto module?
Issue Analytics
- State:
- Created 2 years ago
- Comments:7
Glad you got it working!
Not sure either. But I figured it out using
responses
, and I’m storing state in an exported, instantiated class likemoto
does. Thank you for the help!