我有一个测试,该测试使用'withInterceptors'方法在调用给定方法之前调用 Controller 的拦截器。我如何断言那些拦截器之一的before()方法返回什么?还是有更好的方法来调用拦截器进行测试?

最佳答案

您可以直接在注入(inject)的拦截器对象上调用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()
}

关于grails - 测试Grails 3拦截器的before方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32190736/

10-14 15:59
查看更多