Here are my snippets
JavaScript
x
22
22
1
module1.py
2
class Client:
3
4
def __init__(self):
5
self.api_client = APIClient()
6
7
def get_resources(self):
8
#this method gets some data
9
#end returns list with dictionaries
10
return [{k1:v1},{k2:v2} ]
11
12
module2
13
config = {}
14
15
def add_config(resource):
16
#process the data pass by resource
17
config[resource[k1]] = data
18
19
def instantiate_config():
20
for item in Client().get_resources()
21
add_config(item)
22
So I want to test this instantiate_config with pytest. Here is my try:
JavaScript
1
11
11
1
@patch('module1.Client.get_resources')
2
def test_instantiate_config(self, client_mock):
3
4
dummy_data = {some_dummy_data}
5
#it is a copy of the list, returned form Client().get_resources()
6
7
client_mock.get_resources.returned_values = dummy_data
8
instantiate_config()
9
10
assert 'key1' in config #config is the same config from module2
11
But this gives empty config dict. I don’t know is that possible – to mock Client().get_resources() to give it same value and that value to be passed automatically as argument to add_config_func. If it it not what is the best way to test instantiate_config function
. Not sure is in clear or not, cuz it is a little bit long story
Advertisement
Answer
Your mock is already representing the method get_resources, and you have a typo in “returned_values”. Change to : client_mock.return_value = dummy_data