Unit testing 将Spock单元测试从Grails1.3.9升级到Grails2.3.9。但是edit()测试失败了

Unit testing 将Spock单元测试从Grails1.3.9升级到Grails2.3.9。但是edit()测试失败了,unit-testing,grails,spock,Unit Testing,Grails,Spock,我正在更新Grails项目中的单元测试。我们最初使用的是版本1.3.9,现在我们正在更新到版本2.3.9。我用的是斯波克 我不断地发现这个错误: results: junit.framework.AssertionFailedError: Condition not satisfied: controller.edit() == [filterCategoryInstance: filterCategoryInstance] | | |

我正在更新Grails项目中的单元测试。我们最初使用的是版本1.3.9,现在我们正在更新到版本2.3.9。我用的是斯波克

我不断地发现这个错误:

results:
junit.framework.AssertionFailedError: Condition not satisfied:
controller.edit() == [filterCategoryInstance: filterCategoryInstance]
|          |      |                             |
|          null   false                         John
com.xxxxxx.xxxxx.FilterCategoryController@20574000
以下是控制器代码:

@Secured(["hasAnyRole('CM_ADMIN')"])
def edit() {
    def filterCategoryInstance = FilterCategory.get(params.id)
    if (!filterCategoryInstance) {
        flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'dpFilterCategory.label', default: 'FilterCategory'), params.id])}"
        redirect(action: "list")
    }
    else {
        return [filterCategoryInstance: filterCategoryInstance]
    }
}
下面是测试代码:

@Mock([FilterCategory, FilterCategoryTag])
@TestFor(FilterCategoryController)
@TestMixin(DomainClassUnitTestMixin)
class FilterCategoryControllerSpec extends ExtendedControllerSpec {

def 'edit action:  existing FilterCategory'() {
    setup:
        mockI18N(FilterCategoryController)
        params.id = filterCategoryInstance.id

   expect:
        controller.edit() == [filterCategoryInstance: filterCategoryInstance]


    where:
        tag = new FilterCategoryTag(name: 'tag1')
        filterCategoryInstance = new FilterCategory(name: "John", 
            submissionText:"John", sortOrder:0, 'filterCategoryTags': [tag])

}
这是ExtendedControllerSpec代码。我希望我包含了足够的代码:

我查看了以下网页以获得指导:

@Mixin(MetaClassMixin)
class ExtendedControllerSpec extends Specification {
def props

protected void setup() {
    //super.setup()

    props = new Properties()
    File file = new File("grails-app/i18n/messages.properties")
    if (file.exists()) {
        def stream = new FileInputStream(file)
        props.load stream
        stream.close()
    }

    mockI18N(controller)
}

def mockI18N = { controller ->
    controller.metaClass.message = { Map map ->
    if (!map.code)
        return ""
    if (map.args) {
        def formatter = new MessageFormat("")
        if (props.getProperty(map.code)) {
            formatter.applyPattern props.getProperty(map.code)
        }
        return formatter.format(map.args.toArray())
    } else {
        if (props && props.hasProperty(map.code)) {
            return props.getProperty(map.code)
        } else {
            return map.code
        }
    }
}

}

/**
 * add dynamic methods in test setup.
 */
protected void  addDynamicMethods() {
    registerMetaClass(String)
    String.metaClass.mixin StringUtils

}

protected GrailsUser mockGrailsUser() {
    return Mock(GrailsUser)
}

 ...

/**
 * must call AFTER mockDpSercurityService
 */

protected void setHasRoleTrue() {
    if (controller?.dpSecurityService?.metaClass) {
       controller.dpSecurityService.metaClass.hasRole = {return true}
    }

}
protected void setHasRoleFalse() {
    if (controller?.dpSecurityService?.metaClass) {
       controller.dpSecurityService.metaClass.hasRole = {return false}
    }

}


protected void mockUserService() {
    controller.dpUserService =  new MockFor(UserService)
}
}


看起来是在编辑中执行if分支而不是else分支,因为未保存FilterCategory,因此未获得正确的id。

在其中创建FilterCategory状态后,需要将其保存到数据库中。您可以使用它来快速构建您的实例。感谢您提供的信息,我非常感谢。你能给我举一个使用你提到的插件的例子吗?我如何保存它?再次感谢你的帮助。我试图对该对象调用save,但它不起作用。ExtendedControllerSpec看起来怎么样?我添加了来自ExtendedControllerSpec的代码。@Dave,如果该对象不起作用,您应该检查类似以下的错误:objInstance.errors.allErrors.each{println it},以查看缺少什么。对于构建测试插件,您应该看看文档,它很容易理解!只需要调用DomainClass.build,例如:FilterCategoryTag.buildname:'tag1'您是对的,执行的是if分支,而不是else分支。如何保存FilterCategory?我试着打电话给save,但没用。save返回了什么?如果为真,您可能需要冲洗它。如果为false,则可能是验证错误。设置FailOneError以获取验证异常,请参见