jeudi 3 janvier 2019

Can I use part of the implementation to build test expectation?

Let's suppose that I use build_help_message many times throughout my application and it returns a big dictionary which contains text and attachments which I need to send using Client library.

Is it okay to use build_help_message to build expected result in the test? How can I avoid doing that, if it's not a good practice?

def help_handler(payload):
    team_id = payload['team_id']
    user_id = payload['user_id']

    message = build_help_message(team_id, user_id)

    Client(team_id).send_message(user_id, **message)

Tests

class TestHandler(TestCase):
    def setUp(self):
        team = Team.objects.create(team_id='TEAMID')
        User.objects.create(team=team, user_id='USERID')

    def tearDown(self):
        ...

    @mock.patch('client.Client.send_message')
    def test_correct_text(self, send_message_mock):
        payload = {'team_id': 'TEAMID', 'user_id': 'USERID'}

        handle_message(payload)

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

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

Aucun commentaire:

Enregistrer un commentaire