将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
问题描述
我一直收到这个错误:
结果:
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)
}
}
- 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
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: Here is the controller code: and here is the test code: And here is the ExtendedControllerSpec code. I hope I have included enough code: I have looked at the following web pages for guidance: }如果分支在
edit()
中执行,而不是 else
分支,因为 FilterCategory
没有得到保存,因此无法获得正确的ID。 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])
}
@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屋!