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

时间:2014-01-05 01:04:12

标签: grails grails-filters

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

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

我正在使用Grails 2.3.4

使用Grails Unit Testing guide

我创建了一个过滤器测试:

@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: [:]

0 个答案:

没有答案