单元测试UrlMapping

时间:2015-05-11 10:10:38

标签: grails grails-2.0

我正在使用Grails V2.4.3,并且在为我的UrlMappings传递单元测试时遇到问题。我有以下UrlMappings.groovy

class UrlMappings {

static mappings = {
    "/$controller/$action?/$id?(.$format)?"{
        constraints {
            id(matches: /\d+/)
        }
    }

    "/api/myController/$id"(parseRequest: true) { //my custom url mapping that is failing unit test
      controller = "MyController"
      action = [GET: "getMethod"]
      constraints {
        id(matches: /\d+/)
      }
    }

我的单元测试如下:

import com.demo.MyController
import grails.test.mixin.Mock
import grails.test.mixin.TestFor
import spock.lang.Specification

@TestFor(UrlMappings)
@Mock(MyController)
class UrlMappingsSpec extends Specification {

    //FIXME: Fails reverse mapping assertion. Why?
    void "test url mappings"() {
        expect:       
        assertUrlMapping("/api/myController/1", controller: 'myController', action: "getMethod") {
          id = "1"
       }
    }

}

它以junit.framework.ComparisonFailure: reverse mapping assertion for {controller = myController, action = getMethod, params = [:]} expected:</[api/myController/1]> but was:</[myController/getMethod]>

失败

我可以知道如何让反向映射工作吗?感谢。

0 个答案:

没有答案