繁体   English   中英

Grails单元测试导致空实例

[英]Grails unit test resulting in null instance

我正在尝试进行单元测试以用于脚手架。 我对原始脚手架模板进行了一些更改,以满足我的应用程序所需。 这是测试的代码:

void "Test that the show action returns the correct model"() {
    when:"A domain instance is passed to the show action"
        populateValidParams(params)
        def domainName = new DomainName(params)
        controller.show(domain.toString())

    then:"A model is populated containing the domain instance"
        model.domainNameInstance == domainName
}

这是show操作的代码,其中我根据id中的两个值从数据库中获取域实例,该id是由控制器内的toString方法创建的:

def show(String id){
    if (id!=null){
        def (term, college)=id.split('_')
        DomainName domainNameInstance
        respond domainNameInstance=DomainName.findByVal1AndVal2(val1, val2)
    }else{
        response.sendError(HttpServletResponse.SC_NOT_FOUND)
    }
}

我遇到的问题是,当我运行测试时,测试中domainInstance为null,而域返回正确的值。 有人知道我需要做些什么才能使测试内的domainInstance不为null吗? 作为参考,我正在使用Grails 2.4.3和Scaffolding 2.1.2插件。

在单元测试中,您需要保存要创建的DomainName实例。 您的show动作正在查询数据库,但是您没有持久化测试实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM