mercredi 2 janvier 2019

Correct way to test a function

Is it okay to call build_admins_message in the tests to build expected result that will be used in mock assertion?

Implementation:

@slack_messages.on_pattern('(?i)^admins$')
def handle_admins_message(event, body, match):
    team_id = event['team_id']
    user_id = body['user']

    message = build_admins_message(team_id, user_id)
    Slack(team_id).send_message(user_id, **message)

Tests:

class TestAdminsMessageHandler(TestCase):
    def setUp(self):
        team = SlackTeam.objects.create(team_id='TEAMID')
        SlackUser.objects.create(team=team, user_id='USERID')

    def tearDown(self):
        SlackUser.objects.all().delete()
        SlackTeam.objects.all().delete()

    @mock.patch('slango.slack.Slack.send_message')
    def test_correct_text(self, send_message_mock):
        event = {
            'team_id': 'TEAMID',
            'event': {
                'text': 'admins',
                'user': 'USERID'
            }
        }

        handle_admins_message(event, event['event'])

        expected_message = build_admins_message('TEAMID', 'USERID')

        send_message_mock.assert_called_with('USERID', **expected_message)

Aucun commentaire:

Enregistrer un commentaire