问题描述
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我想这就是你要问的
@OPALi think that's what u asking
Class UserAuthService {
class UserAuthService {
def getCurrentUser(){ def用户= springSecurityService.currentUser 回头用户 }}最后,我收到以下错误:
def getCurrentUser(){ def user = springSecurityService.currentUser return user }}And finally I receive the following error:
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)
推荐答案
我没有看到经过验证的答案,也许会对其他人有所帮助:
I don't see validated answer, maybe it will help somebody else :
使用grails,如果您需要userAuthService或其他服务(例如grails核心服务或您自己的服务),则可能有以下几种可能性:
With grails, if you want userAuthService or other services (like grails core services or your own) you have several possibilities like :
- 在单元测试中存根/模拟它们并定义行为
- 使单元测试成为集成测试
1.在单元测试中存根/模拟它们
在这里有存根:
void "test timeSheets action"() {
given:
...your code...
def userAuthService = Stub(UserAuthService)
User currUser = new User(email: "[email protected]", firstname: "firstname")
// Here we define the behaviour of our Stubbed service :
userAuthService.getCurrentUser() >> currUser
and:"we inject our fake userAuthService"
controller.userAuthService = userAuthService
when:"When Project Id Given"
...your code...
then:
...your code...
}
注意:您可以使用Mock(UserAuthService)代替Stub(UserAuthService).然后,您将能够在然后"部分中验证getCurrentUser()已被调用过一次
NOTE : you can use Mock(UserAuthService) in place of Stub(UserAuthService). Then you'll be able to verify in the "then" part that getCurrentUser() has been called once with
1 * userAuthService.getCurrentUser()
2.使您的单元测试成为集成测试
从有关测试的grails文档中
From grails documentation about Testing
这篇关于在使用Spring Security的Grails Spock单元测试中获得当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!