我有一个测试,使用'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()
}