如何测试更新模型的Grails过滤器?
如何检查单元测试中过滤器更新的模型?
我正在使用Grails 2.3.4
我创建了一个过滤器测试:
@TestMixin(GrailsUnitTestMixin)
@TestFor(MyController)
@Mock(MyFilters)
class MyFiltersTest {
@Test
void myFilterAddsAModelEntry(){
def model
withFilters(action:"index"){
model=controller.index()
}
println "model in test: $model"
assert model.filterObject == "hello world"
}
}
但是,测试失败,因为返回的模型不包含filterObject,即使调用了过滤器。
使用以下控制器:
class MyController {
def index(){
println "MyController.index() called"
// return empty model
[:]
}
和过滤器:
class MyFilters {
def filters = {
all(controller:'*', action:'*') {
before = {
println "before filter called"
}
after {Map model ->
println "after filter called"
model.filterObject="hello world"
println "filtered model: $model"
model
}
afterView = { Exception e -> }
}
}
}
测试生成以下输出:
before filter called...
MyController.index() called...
after filter called
filtered model: [filterObject:hello world]
model in test: [:]