grails测试url参数

时间:2013-10-09 15:31:28

标签: url grails testing mapping

鉴于此映射:

"/student/degree/$studentid/$degreecode"(controller:'student', action:'degree')

我正在尝试下面的测试,但都失败了AssertionFailedError: '...did not match any mapping',两者都是有效的URL。我可以使用仅仅是'/ student / degree'的测试URL取得成功,我认为这应该是失败的,因为参数是必需的。

似乎assertURL方法没有处理多个参数。这只适用于'id',是否无法为这些URL创建测试?

@TestFor(UrlMappings)
@Mock([StudentController])
class UrlMappingsTests {
    void testUrlMappings() {
        assertForwardUrlMapping("/student/degree/102345678/N", controller: "student", action: "degree") {
            assertForwardUrlMapping("/student/degree/102345678/N", controller: "student", action: "degree") {
                studentid = 102345678
                degreecode = "N"
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用Grails 2.2.4测试,它适用于我,我只是添加一个自定义映射类:

package test

class MyUrlMappings {
  static mappings = {
    "/test/show/$myId/$myVal"(controller: "test", action: "show")
  }
}

import spock.lang.*
import test.MyUrlMappings

@TestFor(MyUrlMappings)
@Mock([TestController])
//I'm using Spock
class MyUrlMappingsSpec extends Specification {

  void "mapping should consider myId and myVal"() {
    expect:
      assertForwardUrlMapping("/test/show/1/tst", controller: "test", action: "show") {
        //all params considered as String
        myId = '1'
        myVal = 'tst'
      }
  }

}