【发布时间】:2014-08-08 06:34:38
【问题描述】:
package com.rohan
import grails.test.mixin.*
import spock.lang.Specification
import grails.plugins.springsecurity.SpringSecurityService
import grails.test.mixin.domain.DomainClassUnitTestMixin
import com.rohan.meta.MetaServiceCategory
@TestFor(ProjectDashBoardController)
@Mock([SpringSecurityService,User,Project,MetaServiceCategory,ProjectIntroduction,ProjectDetails,ProjectAdditionalInfo,SPCompany,BuyerCompany,Conversation,ServiceCategory,UserAuthService,TimeSheetService,ProjectService])
class ProjectDashBoardControllerSpec extends Specification {
void "test timeSheets action"() {
given:
BuyerCompany buyerCompany = new BuyerCompany()
Project testProject = new Project(title: "test Project",projectId:"0411",buyerCompany:buyerCompany)
testProject.save()
println "project : "+testProject
when:"When Project Id Given"
controller.params.id = "00411"
controller.timeSheets()
then:
response.redirectUrl.endsWith '/dashboard/index'
}
}
这是控制器代码:
class ProjectDashBoardController {
def springSecurityService
def userAuthService
def timeSheets(){
def project=Project.findByProjectId(params.id)
if(project){
def user = userAuthService.getCurrentUser()
// Do Something
} else {
redirect(controller:'dashboard',action:'index')
}
}
}
@OPAL 我想这就是你要问的
类 UserAuthService {
def getCurrentUser(){ def 用户 = springSecurityService.currentUser 返回用户 } } 最后我收到以下错误:
Cannot get property 'currentUser' on null object
at com.rohan.UserAuthService.getCurrentUser(UserAuthService.groovy:22)
at com.rohan.ProjectDashBoardController.timeSheets(ProjectDashBoardController.groovy:130)
at com.rohan.ProjectDashBoardControllerSpec.test timeSheets action(ProjectDashBoardControllerSpec.groovy:29)
【问题讨论】:
标签: spring-security grails-plugin spock