I'm currently trying to find a good way to mock multiple layers / nested return values. In other words, I want to return the magic layout, which in turn returns the magic layout with its own return values. I find this relatively cumbersome and am looking for a more elegant and maintainable solution.
I am trying to effectively test the following code. The URL returns a json string that needs further processing:
import json from urllib.request import url open def load_json():
Here's how I still mocked this, which is extremely inefficient and makes maintenance difficult:
class MyTestCase(TestCase): @patch('load_json_path.urlopen') def test_load_json(self, mock_urlopen):
In the end, all I'm trying to make fun of is the returned data from the decoding function, which comes from the URL opening function. This should be possible on a single line or in a simpler way, using perhaps the input methods. Ideally, the layout will look something like this in the test_load_json function:
mock_urlopen.__enter__.loads.__enter__.decode.return_value = '["myjsondata"]'
Unfortunately, I cannot find anything useful in the mock documentation. Any help was appreciated.
Ric w source share