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

时间:2015-08-24 19:58:58

标签: grails

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

1 个答案:

答案 0 :(得分:2)

您可以直接在注入的拦截器对象

上调用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()
}