简体   繁体   English

将Spock单元测试从Grails 1.3.9升级到Grails 2.3.9。 但是edit()测试失败

[英]Upgrading Spock unit tests from Grails 1.3.9 to Grails 2.3.9. But edit() test is failing

I am updating unit tests in a Grails project. 我正在Grails项目中更新单元测试。 We were originally using version 1.3.9 and now we are updating to version 2.3.9. 我们最初使用的是1.3.9版,现在我们正在更新至2.3.9版。 I am using Spock. 我正在使用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. 这是ExtendedControllerSpec代码。 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)
}

} }

看起来if分支是在edit()执行的,而不是else分支,因为FilterCategory没有得到保存,因此没有获得正确的ID。

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

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