Grails控制器单元测试不会将页面呈现给response.text [英] Grails Controller Unit Test doesn't render page to response.text

查看:95
本文介绍了Grails控制器单元测试不会将页面呈现给response.text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的环境配置:Java 1.7u51,Grails 2.3.7



我试图断言 response.text 在Controller Test中,但它总是带。



发生了什么?

这是我的UserController

  class UserController {

def index(){
flash.errors =$ params .secret
render view:index,model:[model:params.toModel,
text:params.username]
}
}

这是/user/index.gsp文件

  $ {text} 

这是我的规格

  @TestFor(UserController)$ b $ class UserControllerSpec extends Specification {

def setup(){
}

def cleanup(){
}

voidtest something(){

给出:
params.username = USERNAME
params.password =SECRET
params.toModel =Model

当:
controller.index()

then:
flash.errors
view == / user / index
params.username == response.text
model.model == params.toModel
}
}


和测试报告是:

 失败: | 
测试一些东西(teste.UserControllerSpec)
|
条件不满足:

params.username == response.text
| | | | |
| USERNAME | |
| | org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletResponse@46f29a61
|假
| 8差异(0%相似度)
| (USERNAME)
| (-------)
[用户名:USERNAME,密码:SECRET,toModel:Model]


解决方案

只有模板呈现的情况下,模板的内容直接呈现为字符串响应。因此 response.text 只能在渲染模板的时候使用,与这种情况下渲染视图不同。

为了测试渲染视图, GroovyPageUnitTestMixin 必须如下使用:

  import grails.test.mixin.TestMixin 
import spock.lang.Specification
import grails.test.mixin.web.GroovyPageUnitTestMixin

@TestMixin(GroovyPageUnitTestMixin)
class UserControllerSpec extends Specification {
def controller
$ b def setup(){
controller = testFor(UserController)
}

voidtest something(){
given:
params.username =USERNAME
params.password =SECRET
params.toModel =模型

当:
controller.index()

then:
flash.errors
view ==/ user / index
model.model == params.toModel

//提供的方法由mixin模仿
//控制器中的渲染方法。确保模型也传递到地图arg
//因为视图使用了model.text

render(view:/ user / index,model:model)=='USERNAME'


注意:




  • @TestFor 被测试中的Mixin替换。因此,必须使用 testFor()方法来模拟控制器,如 setup()中所示。

  • render()也可用于测试模板呈现,其中键模板类似于在地图参数中使用关键 view


My env configs: Java 1.7u51, Grails 2.3.7

I'm trying to assert response.text in Controller Test but it always brings "".

What's happening?

This is my UserController

class UserController {

    def index() {
        flash.errors = "$params.secret"
        render view: "index", model: [model: params.toModel, 
                text: params.username]
    }
}

This is /user/index.gsp file

${text}

This is my Specification

@TestFor(UserController)
class UserControllerSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {

        given:
            params.username = "USERNAME"
            params.password = "SECRET"
            params.toModel = "Model"

        when: 
            controller.index()

        then: 
            flash.errors
            view == "/user/index"
            params.username == response.text
            model.model == params.toModel
    }
}

And test report is:

Failure:  |
test something(teste.UserControllerSpec)
 |
Condition not satisfied:

params.username  == response.text
|      |         |  |        |
|      USERNAME  |  |        ""
|                |  org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletResponse@46f29a61
|               false
|               8 differences (0% similarity)
|               (USERNAME)
|               (-------)
[username:USERNAME, password:SECRET, toModel:Model]

解决方案

It is only the case of a template rendering where the content of the template is directly rendered to response as String. Hence response.text can only be used when a template is rendered unlike in this case where a view is being rendered.

In order to test rendering a view, GroovyPageUnitTestMixin has to be used as below:

import grails.test.mixin.TestMixin
import spock.lang.Specification
import grails.test.mixin.web.GroovyPageUnitTestMixin

@TestMixin(GroovyPageUnitTestMixin)
class UserControllerSpec extends Specification {
    def controller

    def setup(){
        controller = testFor(UserController)
    }

    void "test something"() {
        given:
            params.username = "USERNAME"
            params.password = "SECRET"
            params.toModel = "Model"

        when:
            controller.index()

        then:
            flash.errors
            view == "/user/index"
            model.model == params.toModel

            //method provided by mixin which mimics render method in 
            //controller. Make sure model is also passed in the map arg
            //since view uses model.text

            render(view: "/user/index", model: model) == 'USERNAME'
    }
}

Note:

  • @TestFor is replaced with the Mixin in the test. Therefore, controller has to be mocked as seen in setup() using testFor() method.
  • render() can also be used to test template rendering with the key template similar to the usage of key view in the map argument.

这篇关于Grails控制器单元测试不会将页面呈现给response.text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆