Grails的文檔狀態:
Grails的不調用攔截器或者在集成測試期間調用操作時使用servlet過濾器。您應該單獨測試攔截器和過濾器,必要時使用功能測試。
這也適用於單元測試,您的控制器操作不受定義的攔截器的影響。
既然你有:
def afterInterceptor = [action: this.&interceptAfter, only: ['actionWithAfterInterceptor','someOther']]
private interceptAfter(model) { model.lastName = "Threepwood" }
要測試的攔截器,你應該:
驗證攔截應用到所需的操作
void "After interceptor applied to correct actions"() {
expect: 'Interceptor method is the correct one'
controller.afterInterceptor.action.method == "interceptAfter"
and: 'Interceptor is applied to correct action'
that controller.afterInterceptor.only, contains('actionWithAfterInterceptor','someOther')
}
驗證攔截方法有預期效果
void "Verify interceptor functionality"() {
when: 'After interceptor is applied to the model'
def model = [firstName: "Guybrush"]
controller.afterInterceptor.action.doCall(model)
then: 'Model is modified as expected'
model.firstName == "Guybrush"
model.lastName == "Threepwood"
}
或者,如果您有沒有攔截,確認沒有任何
void "Verify there is no before interceptor"() {
expect: 'There is no before interceptor'
!controller.hasProperty('beforeInterceptor')
}
那些例子是用於攔截後測試,但攔截之前同樣應該適用於也。