简体   繁体   English

测试功能的正确方法

[英]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? 可以在测试中调用build_admins_message来构建将在模拟断言中使用的预期结果吗?

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)

Implementation of the build_admins_message : build_admins_message实现:

def build_admins_message(team_id, user_id):
    user = SlackUser.retrieve(team_id, user_id)
    admins = SlackUser.objects.filter(
        is_bot_admin=True, team__team_id=team_id).order_by(
            'real_name', 'display_name')

    attachments = []
    if user.is_bot_admin:
        attachments.append(build_admin_picker())

    for admin in admins:
        attachments.append(build_admin_item(user, admin))

    attachments.append(build_admin_more())

    return {
        'text': "Here is users with admin rights:",
        'attachments': attachments
    }

This would depend on the role of build_admins_message in your program. 这将取决于build_admins_message在程序中的角色。

Since different parts of the program all need to build the message in the same way, this is probably okay. 由于程序的不同部分都需要以相同的方式构建消息,所以这可能是可以的。 Consider whether you can make it more explicit that build_admins_message is used like this, eg with dependency injection. 考虑一下是否可以使build_admins_message像这样通过依赖注入来更明确地使用。 Make sure your helper method has its own test. 确保您的辅助方法具有自己的测试。 (I usually see using patch as a design smell, but remember that doesn't automatically mean something is wrong!) (我通常会看到使用patch作为设计气味,但请记住,这并不意味着有问题!)

If instead build_admins_message existed solely as a helper function for handle_admins_message , then using it in your test violates encapsulation and ties your test to the implementation too much. 相反,如果build_admins_message仅作为build_admins_message的辅助函数存在,则在测试中使用它会handle_admins_message封装并将测试与实现过多地联系handle_admins_message In that case, I would just manually write out the expected message in my test. 在这种情况下,我只需要在测试中手动写出预期的消息即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM