将Grails 1.3.9中的Spock单元测试升级到Grails 2.3.9。但编辑()测试失败 [英] Upgrading Spock unit tests from Grails 1.3.9 to Grails 2.3.9. But edit() test is failing

查看:169
本文介绍了将Grails 1.3.9中的Spock单元测试升级到Grails 2.3.9。但编辑()测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我一直收到这个错误:

 结果:
junit.framework.AssertionFailedError:条件不满足:
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扩展ExtendedControllerSpec {

def'编辑动作:现有FilterCategory'(){
设置:
mockI18N(FilterCategoryController)
params.id = filterCategoryInstance.id

期望:
controller.edit() == [filterCategoryInstance:filterCategoryInstance]


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

}

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



我看过以下网页以获得指导:

<$ p $
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(控制器)
}

def mockI18N = {controller - >
controller.metaClass.message = {地图 - >
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.hasProperty(map.code)){
返回props.getProperty(map.code)
} else {
return map。代码
}
}
}

}

/ **
*在测试设置中添加动态方法。
* /
protected void addDynamicMethods(){
registerMetaClass(String)
String.metaClass.mixin StringUtils



protected GrailsUser mockGrailsUser(){
return Mock(GrailsUser)
}
$ b $ ...

/ **
*必须调用AFTER mockDpSerServiceService
* /

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


$ b保护无效setHasRoleFalse(){
if(controller?.dpSecurityService?.metaClass){
controller.dpSecurityService。 metaClass.hasRole = {返回false}
}

}


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

}




解决方案

如果分支在 edit()中执行,而不是 else 分支,因为 FilterCategory 没有得到保存,因此无法获得正确的ID。

I am updating unit tests in a Grails project. We were originally using version 1.3.9 and now we are updating to version 2.3.9. I am using Spock.

I keep getting this error:

results:
junit.framework.AssertionFailedError: Condition not satisfied:
controller.edit() == [filterCategoryInstance: filterCategoryInstance]
|          |      |                             |
|          null   false                         John
com.xxxxxx.xxxxx.FilterCategoryController@20574000

Here is the controller code:

@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]
    }
}

and here is the test code:

@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])

}

And here is the ExtendedControllerSpec code. I hope I have included enough code:

I have looked at the following web pages for guidance:

@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)
}

}

解决方案

Looks like the if branch gets executed in edit() instead of the else branch because FilterCategory does not get saved and therfore does not get a proper id.

这篇关于将Grails 1.3.9中的Spock单元测试升级到Grails 2.3.9。但编辑()测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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