我的单元测试有问题。这是我的单元测试:
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)
我似乎无法弄清楚为什么我对该特定方法的单元测试仍然失败。也许我的命令对象模拟是关键?提前谢谢。
编辑:条件应该是' response.redirectedUrl ==" / role / index"',我只是复制了错误的消息,因为我当时正在摆弄代码。
编辑:这是控制器方法:
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]
}
}
答案 0 :(得分:0)
您尝试测试的应用似乎可以设计得更好。许多逻辑可以从控制器转移到服务(separation of concerns原则)。
此外,您的测试看起来像集成测试,而不是单元。所以我建议你熟悉interaction based testing。
祝你好运!