Created
November 19, 2019 20:52
-
-
Save imakecodes/7098c65f63748770f26ac1508e86134d to your computer and use it in GitHub Desktop.
Este snippet foi obtido em https://alysivji.github.io/mocking-functions-inputs-args.html e mostra como mockar um recurso e de acordo com os parâmetros de entrada do recurso retornar diferentes valores
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# test_calc_stats.py | |
import calc_stats | |
user_data = [ | |
{ 'id': 1, 'name': 'Aly', 'email': 'alysivji@gmail.com'}, | |
] | |
activity_data = [ | |
{ 'id': 65, 'description': 'morning jog', 'distance': 3.1 }, | |
{ 'id': 66, 'description': 'lunch break', 'distance': 1.2 }, | |
{ 'id': 67, 'description': 'weekend long run', 'distance': 6.2 }, | |
{ 'id': 68, 'description': 'night run', 'distance': 2.5 }, | |
] | |
def load_data(endpoint, *args, **kwargs): | |
if 'user' in endpoint: | |
return user_data | |
elif 'activity' in endpoint: | |
return activity_data | |
def test_motivation_message(mocker): | |
# Arrange | |
mock_api = mocker.MagicMock(name='api') | |
mock_api.get.side_effect = load_data | |
mocker.patch('calc_stats.fitness_api', new=mock_api) | |
# Act | |
result = calc_stats.motivation_message('alysivji@gmail.com') | |
# Assert | |
assert result == f'Aly has run {3.1 + 1.2 + 6.2 + 2.5} miles' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment