小编Rod*_*Rod的帖子

Grails控制器在渲染模板时测试关于模型的断言?

使用Grails 2.1.0

似乎从控制器执行此操作:

render(view: "someView", model: [modelEntry: "hello"]) 
Run Code Online (Sandbox Code Playgroud)

允许我在该控制器的单元测试中执行此操作:

controller.method() 
assert model.modelEntry == "hello" 
Run Code Online (Sandbox Code Playgroud)

但是,如果我更改控制器来执行此操作:

render(template: "someTemplate", model: [modelEntry: "hello"]) 
Run Code Online (Sandbox Code Playgroud)

现在测试中的模型实例是一个空数组.我已经做了很多搜索,并且大多数解决方案似乎都是针对Grails 1的,通常涉及modelAndView对象(我的测试中不存在)或renderArgs(同上).

我发现的唯一解决方案是手动覆盖测试中的视图,如下所示:

views['_someTemplate.gsp'] = '${modelEntry}'
Run Code Online (Sandbox Code Playgroud)

然后对字符串进行断言.但我不喜欢这个解决方案,因为它:

  1. 要求测试知道模板的文件名
  2. 难以测试没有良好toString()方法的模型条目
  3. 难以对相关模型条目进行多次断言.

当控制器呈现模板时,有没有办法从测试用例更直接地获取模型中的条目?

grails

5
推荐指数
1
解决办法
1971
查看次数

通过委托绑定时,Groovy闭包短格式方法调用不起作用?

我创建了一个代码示例,显示了我遇到的问题:

class BindingExample {

    public static void main(String[] args) {

        Closure closure1 = {
            printit.call("Hello from closure 1")
        }

        Closure closure2 = {
            printit("Hello from closure 2")
        }

        Closure printit = { s ->
            println("printing: "+s)
        }

        Binding binding = new Binding()
        binding.setVariable("printit", printit)

        closure1.delegate = binding
        closure2.delegate = binding

        closure1()  //This works fine
        closure2()  //This does not.  

        //Why does .call() work and () alone not?  Most documentation says they're the same.
    }

}
Run Code Online (Sandbox Code Playgroud)

为printit是一个Closure,其中文件指示器具doCall,因此在短形式可调用经由().

但是,当通过绑定到委托来使此闭包可用时,只允许调用的长格式版本.输出是: …

groovy binding

5
推荐指数
1
解决办法
355
查看次数

标签 统计

binding ×1

grails ×1

groovy ×1