我有以下 build_settings_message
函数,我有点困惑应该如何为它编写单元测试。我需要检查它的哪些方面?
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')
}
]
}
最佳答案
你必须 mock SlackTeam
和 Site
并为 team
和 domain
返回一些虚假但看起来真实的值,然后验证build_settings_message
返回的值是正确的(基于 team
和 domain
)。
一定要检查边缘情况,例如没有团队、重复域等。
关于python - 我需要测试功能的哪些方面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54039157/