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

倖福魔咒の 提交于 2019-12-11 18:08:06

问题


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

}

  • http://sanjaykanwar.blogspot.com/2012/07/grails-controller-test-with-spock.html
  • http://naleid.com/blog/2012/05/01/upgrading-to-grails-2-unit-testing

回答1:


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.



来源:https://stackoverflow.com/questions/25516062/upgrading-spock-unit-tests-from-grails-1-3-9-to-grails-2-3-9-but-edit-test-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!