在方法之前测试Grails 3拦截器

  • 本文关键字:Grails 测试 方法 grails
  • 更新时间 :
  • 英文 :


我有一个测试,使用'withInterceptors'方法在调用给定方法之前调用控制器的拦截器。我怎样才能断言其中一个拦截器的before()方法返回?或者是否有更好的方法来调用拦截器进行测试?

你可以直接在注入的拦截器对象上调用before方法

@TestFor(AuthInterceptor)
class AuthInterceptorSpec extends Specification {
    void "Invalid token fails auth"() {
        when:
        withRequest(controller:"book")
        interceptor.request.addHeader(HttpHeaders.AUTHORIZATION,"Bearer FOOBAR")
        then:
        !interceptor.before()
    }
    void "Valid token passes auth"() {
        when:
        withRequest(controller:"book")
        interceptor.request.addHeader(HttpHeaders.AUTHORIZATION,"Bearer someValidToken")
        then:
        interceptor.before()
}

最新更新