Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 带有'的单元测试控制器;格式为';html内容类型给出空响应,除非';渲染';显式调用_Unit Testing_Grails - Fatal编程技术网

Unit testing 带有'的单元测试控制器;格式为';html内容类型给出空响应,除非';渲染';显式调用

Unit testing 带有'的单元测试控制器;格式为';html内容类型给出空响应,除非';渲染';显式调用,unit-testing,grails,Unit Testing,Grails,在Grails2.1.1应用程序中,我尝试对使用 “withFormat”将响应呈现为HTML或JSON。但是,响应HTML内容类型总是导致 测试中的空响应,除非我将其包装在闭包中并显式调用“render”。对于JSON, 它发回预期的响应 控制器: import grails.converters.JSON class TestController { def formatWithHtmlOrJson() { withFormat { html someContent

在Grails2.1.1应用程序中,我尝试对使用 “withFormat”将响应呈现为HTML或JSON。但是,响应HTML内容类型总是导致 测试中的空响应,除非我将其包装在闭包中并显式调用“render”。对于JSON, 它发回预期的响应

控制器:

import grails.converters.JSON

class TestController {
def formatWithHtmlOrJson() {
    withFormat {
        html someContent:"should be HTML" 
        json {render new Expando(someContent:"should be JSON")  as JSON}
    }
}
测试:

如上所述,对于JSON,这是可行的,但是对于HTML,我总是得到一个空值 响应,除非我将html检查包装在闭包中并显式调用render,如下所示:

withFormat {
    html {
        render someContent:"should be HTML" 
    }
医生建议我不需要这样做,例如:

withFormat {
    html bookList: books
    js { render "alert('hello')" }
    xml { render books as XML }
}

令人沮丧的是,关于测试的grails文档提到了withFormat的使用,但只给出了测试xml/json的示例,而没有给出html响应的示例

有人能解释这个差异吗,或者我在我的工作中如何解决这个问题 测试?

试试看

withFormat {
    html {
        println("html")
        [new Expando(test:"html")]
    }
}

文档中提供的建议没有使用
闭包
,而是有这样一句话:“Grails搜索名为Grails app/views/book/list.html.gsp的视图,如果找不到,则返回到Grails app/views/book/list.gsp”

当您对
html
使用
closure
时,我们必须将
模型
返回到操作或
呈现
内容。因为在这种情况下,您使用的是
html
closure
,所以必须呈现内容

使用闭包(您的用例已通过)


终于弄明白了

在文档()中,它提到了在返回映射的测试操作下测试控制器响应的这种方法 :

同样的方法也适用于使用withFormat的控制器方法

因此,对于我上面的原始示例:

withFormat {
    html someContent:"should be HTML" 
...
测试变成:

void testForHtml() {
    response.format = "html"
    def model = controller.formatWithHtmlOrJson()
    assert model.someContent == "should be HTML"
}
文档有点混乱,因为withFormat部分没有提到这种方法

值得注意的是,如果其他人遇到这种情况,则如果html块位于闭包中,则不会返回映射,而是返回映射项的值,因此对于控制器代码:

withFormat{
    html{
        someContent:"should be HTML" 
    }...
测试检查变为:

     assert model == "should be HTML"
或者,如果可以修改控制器代码,则在映射内返回结果可以使用点符号检查元素值。对于此代码:

 withFormat {
    html {
        [someContent:"should be HTML"] 
    }....
测试检查是:

assert model.someContent == "should be HTML"
还值得注意的是,在我的原始示例中,没有对HTML类型使用闭包,不能将值作为映射返回-这会导致编译错误

//Don't do this, won't compile
    withFormat {
        html [someContent:"should be HTML"]         
    ...

您可以发布单元测试的代码吗?在html测试中添加了测试代码并充实了controller code.assert response.json?抱歉,剪切粘贴错误,现已修复。我在这里创建了您的示例,并得到了相同的错误。文档中的代码显示了一个仅使用render的示例(可能是因为这个原因吧?),不幸的是,这没有什么区别。我在帖子中提到,要通过此测试,唯一的方法是显式调用render。我尝试过html内容类型的闭包和不闭包。我在文本格式方面也遇到了同样的问题
response.format=“text”
成功了。非常感谢。
 withFormat {
    html {
        [someContent:"should be HTML"] 
    }....
assert model.someContent == "should be HTML"
//Don't do this, won't compile
    withFormat {
        html [someContent:"should be HTML"]         
    ...