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

时间:2014-08-26 22:18:36

标签: unit-testing grails spock

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

我一直收到这个错误:

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

}

1 个答案:

答案 0 :(得分:0)

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

相关问题