在使用Spring Security的Grails Spock单元测试中获得当前用户 [英] get current user in grails spock unit testing which uses spring security

查看:140
本文介绍了在使用Spring Security的Grails Spock单元测试中获得当前用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 我想这就是你要问的

@OPAL i 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. 在单元测试中存根/模拟它们并定义行为
  2. 使单元测试成为集成测试

1.在单元测试中存根/模拟它们

在这里有存根:

void "test timeSheets action"() {
    given:
    ...your code...
    def userAuthService = Stub(UserAuthService)
    User currUser = new User(email: "current@current.fr", 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

集成测试与单元测试的不同之处在于您拥有完全访问权限 测试中的Grails环境. Grails使用内存中的H2 数据库以进行集成测试,并从中清除所有数据 测试之间的数据库.

Integration tests differ from unit tests in that you have full access to the Grails environment within the test. Grails uses an in-memory H2 database for integration tests and clears out all the data from the database between tests.

这篇关于在使用Spring Security的Grails Spock单元测试中获得当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆