gpt4 book ai didi

unit-testing - Grails单元测试带有命令对象的方法

转载 作者:行者123 更新时间:2023-12-02 15:48:49 26 4
gpt4 key购买 nike

我的单元测试有问题。这是我的单元测试:

void "Test the update action performs an update on a valid domain instance"() {
given:
def aclServiceMock = mockFor(AclService)
aclServiceMock.demand.updateRolePermissions { Role role, Map, roleMap, List permissions -> return true }
controller.aclService = aclServiceMock.createMock()

def roleInstance = setupRoleController()

mockCommandObject RoleCommand
RoleCommand cmd = new RoleCommand(
authority: roleInstance?.authority,
description: roleInstance?.description
)

when:"A valid domain instance is passed to the update action"
controller.update(roleInstance, cmd)

then:"A redirect is issued to the show action"
response.redirectedUrl == "/role/index"
flash.message != null
flash.error == null
}

这是我从终端获得的结果:
| Failure:  Test the update action performs an update on a valid domain instance(ph.gov.doe.core.acl.RoleControllerSpec)
| Condition not satisfied:
flash.message != null
| | |
[:] null false
at ph.gov.doe.core.acl.RoleControllerSpec.Test the update action performs an update on a valid domain instance(RoleControllerSpec.groovy:222)

我似乎无法弄清楚为什么我对特定方法的单元测试一直失败。也许我的Command Object模拟是关键?提前致谢。

编辑:条件应该是'response.redirectedUrl ==“/ role / index”',我只是复制了错误的消息,原因是我当时对代码不满意。

编辑:这是 Controller 方法:
def update(Role roleInstance, RoleCommand roleCommand) {
Map roleMap = [:]
ArrayList collectPermission = [], getPermissions = [], roleList = []
def savedRoleInstance

/** collect selected permission into arraylist */
collectPermission.addAll(params?.selectedPermission ?: [])
getPermissions = Permission.findAllByIdInList(collectPermission)

def roleEditInstance = Role.get(params?.roleId)

/** Set data for validation of Role */
roleCommand.with {
id = roleEditInstance?.id
authority = params?.authority
description = params?.description
}

roleCommand.validate()

/** Check if the set of permission already exists */
roleList = RolePermission.findAllByPermissionInListAndRoleNotEqual(getPermissions, Role.findByAuthority('ROLE_SUPERADMIN'))?.role

def duplicateRolePermission = roleList.find { r -> r.getAuthorities().sort { it?.id } == getPermissions.sort { it?.id } && r != roleEditInstance }

if (collectPermission.isEmpty()) {
flash.error = message(code: 'role.permissions.blank', args: [message(code: 'role.label', default: params?.authority)])
respond roleInstance, model:[roleList: Role.list(), permissionList: Permission.findAllByAuthorityNotEqual('PERM_DASHBOARD_VIEW'), inheritPermission: params?.inheritPermission, selectedPermission: getPermissions], view: "edit"
} else if (roleCommand.hasErrors() || duplicateRolePermission != null) {
bindData(roleCommand, roleInstance)

if(duplicateRolePermission != null){
flash.error = message(code: 'role.permissions.unique', args: [message(code: 'role.label', default: getPermissions?.description)])
}

respond roleCommand.errors, model:[roleInstance: roleCommand,roleList: Role.list(), permissionList: Permission.findAllByAuthorityNotEqual('PERM_DASHBOARD_VIEW'), inheritPermission: params?.inheritPermission, selectedPermission: getPermissions, roleId: roleEditInstance?.id], view: "edit"
} else {
/** Save the Role */
roleMap = [authority: params?.authority, description: params?.description]
def savedRole = aclService.updateRolePermissions(roleEditInstance, roleMap, getPermissions)

if (currentAccount) {
auditLogService.logEvent(currentAccount.emailAddress, "UPDATE_ROLE_SUCCESS", "Successfully updated role details.", true)
}

flash.message = message(code: 'role.updated.message', args: [message(code: 'role.label', default: savedRole?.authority)])
flash.id = savedRole?.id
redirect action: 'view', params:[id: savedRole?.id]
}
}

最佳答案

您尝试测试的应用似乎可以设计得更好。许多逻辑可以从 Controller 转移到服务(separation of concerns原则)。

同样,您的测试看起来像集成测试而不是单元测试。因此,我建议您熟悉interaction based testing

祝好运!

关于unit-testing - Grails单元测试带有命令对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42799886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com