以下是我的beforeInterceptor
def beforeInterceptor = {
log.debug "============${actionName}=========="
if (!(actionName in ['search'])) {
redirect controller: 'error', action: 'denied'
return false
}
}
我不知道如何编写 beforeInterceptor
的测试。我用谷歌搜索但没有运气。我已经尝试
自己编写它的测试用例,但无法在 beforeInterceptor 中获取 actionName
。
when:
controller.beforeInterceptor()
或
when:
controller.beforeInterceptor.call()
controller.history(ticket)
每次我在 actionName
中得到 null。
有人知道:如何编写 beforeInterceptor 的测试用例(integration
或 unit
)?
最佳答案
我为此创建了单元测试,我们可以在那里的请求中设置操作名称。我直接调用了 beforeInterceptor:
@TestFor(PersonController)
@Mock([Person])
class PersonControllerSpec extends Specification {
def setup() {
}
def cleanup() {
}
void "test beforeInterceptor"() {
when:
webRequest.actionName = "index"
controller.beforeInterceptor()
then:
controller.response.redirectedUrl == '/error/denied'
when:
controller.response.reset()
webRequest.actionName = "search"
controller.beforeInterceptor()
then:
controller.response.redirectedUrl != '/error/denied'
}
}
关于unit-testing - beforeInterceptor 的测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24012938/