Mocking a whole class - Fixtures and Mocking in Python
Keyboard shortcuts
Press ← or → to navigate between chapters
Press S or / to search in the book
Press ? to show this help
Press Esc to hide this help
Mocking a whole class
import json
class Thing(object):
def data_file():
return "/corporate/fixed/path/data.json"
def get_sum(self):
data_file = self.data_file()
with open(data_file) as fh:
data = json.load(fh)
# ...
result = data['x'] + data['y']
return result
{
"x": 19,
"y": 23
}
import app
def test_sum():
app.Thing.data_file = lambda self: 'data.json'
t = app.Thing()
res = t.get_sum()
assert True
assert res == 42