如何对更新模型的Grails After过滤器进行单元测试

如何对更新模型的Grails After过滤器进行单元测试,grails,grails-filters,Grails,Grails Filters,如何测试更新模型的Grails过滤器? 如何在单元测试中检查由过滤器更新的模型? 我正在使用Grails2.3.4 使用 我创建了一个过滤器测试: @TestMixin(GrailsUnitTestMixin) @TestFor(MyController) @Mock(MyFilters) class MyFiltersTest { @Test void myFilterAddsAModelEntry(){ def model withFilters(action:"

如何测试更新模型的Grails过滤器?

如何在单元测试中检查由过滤器更新的模型?

我正在使用Grails2.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: [:]

存在多个问题:第一个
model=controller.index()
之后的
过滤器之前调用。第二:我希望在测试的
model
属性中更改模型,就像在普通控制器测试中一样(您不需要创建它)。但这不起作用,因为
with filter
在将模型传递给过滤器之前会“复制”模型。因此,你永远不会得到修改后的模型。马丁豪纳,谢谢你的评论。经过进一步调查,我得出了同样的结论。我将研究创建一个bug标签,并发布一个建议的解决方案。