简体   繁体   中英

What aspects of a function do I need to test?

I have the following build_settings_message function and I am a bit confused how should I write a unit test for it. What aspects of it do I need to check?

def build_settings_message(team_id):
    team = SlackTeam.objects.find_by_id(team_id)
    domain = Site.objects.get_current().domain

    attachments = [
        _build_manage_admins(),
        _build_checks_available(team, domain)
    ]

    return {
        'text': "Here is my settings page",
        'attachments': attachments
    }


def _build_manage_admins():
    return {
        "fallback": "Manage admins",
        "color": "#85cdff",
        "callback_id": "admins_controls",
        "title": "Admins",
        "footer": "Users that could remove and edit any checks ",
        "actions": [
            {
                "name": "manage",
                "text": ":key: Manage Admins",
                "type": "button",
                "value": "manage"
            }
        ]
    }


def _build_checks_available(team, domain):
    return {
        "title": "Items available",
        "footer": ("You have got *{} of {}* items for "
                   "check *available*.").format(
                       team.checks_used, team.checks_available),
        "actions": [
            {
                "text": "Open Dashboard",
                "type": "button",
                "url": 'https://' + domain + reverse('dashboard')
            }
        ]
    }

You have to mock SlackTeam and Site and return some fake, but real-looking values for team and domain , then verify that the value returned by build_settings_message is correct (based on team and domain ).

Be sure to check edge-cases such as no team, duplicate domain, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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