【发布时间】:2019-06-29 11:29:50
【问题描述】:
出于演示目的,我使用这些文件设置了一个全新的 grails 应用程序:
class HalloController {
def index() {
String heading = request.getAttribute("heading")
render "${heading}"
}
}
class HalloInterceptor {
boolean before() {
request.setAttribute("heading", "halloechen") // *** set breakpoint here***
true
}
boolean after() { true }
void afterView() {
// no-op
}
}
当我到达http://localhost:8080/hallo 时,“halloechen”被打印出来,因为它被设置为拦截器before() 方法中的请求属性,就像我想要的那样。
现在我想对拦截器进行单元测试:
class HalloInterceptorSpec extends Specification implements InterceptorUnitTest<HalloInterceptor> {
def setup() {
}
def cleanup() {
}
void "Test hallo interceptor matching"() {
when:"A request matches the interceptor"
withRequest(controller:"hallo")
then:"The interceptor does match"
interceptor.doesMatch() && request.getAttribute("heading") == "halloechen"
}
}
此测试失败,因为 heading 属性未设置为请求(无论如何这是一个模拟请求)。事实上,在运行单元测试时,拦截器似乎甚至没有被调用。我在before() 方法中设置了一个断点,在调试测试时我从来没有到达那里。这很奇怪,因为我希望拦截器测试至少调用拦截器。
我知道我可以按照here 的描述重写测试,但我的意思是拦截器根本不会被调用。
那正确吗?另一件事:在测试中调用getModel() 总是返回null。如何在我的测试中获取模型?
【问题讨论】:
标签: java unit-testing grails grails-3.0 grails-3.3.x