测试资源控制器的UrlMapping路径

时间:2014-08-18 04:47:15

标签: grails grails-controller

在Grails 2.3及以上版本中,现在有资源控制器的概念。但是,它们似乎不适用于assertUrlMappings概念。

/foo (resources:"foo")

然后在url-mappings-report中出现以下内容:

Controller: foo
|   GET    | /foo            | Action: index  
|   GET    | /foo/create    | Action: create                                 
|   POST   | /foo            | Action: save   
|   GET    | /foo/${id}      | Action: show   
|   GET    | /foo/${id}/edit | Action: edit   
|   PUT    | /foo/${id}      | Action: update 
|  PATCH   | /foo/${id}      | Action: patch  
|  DELETE  | /foo/${id}      | Action: delete 

但是,在以下文件中,无法测试网址:

@TestFor(UrlMappings)
@Mock(FooController)
class FooURIReverseMappingsSpec extends Specification {

    def "Ensure basic mapping operations for Foo uris"() {
        expect:
        assertUrlMapping(url, controller: expectCtrl, action: expectAction) {
            id = expectId
        }
        where:
        url |   expectCtrl  | expectAction | expectId
        // Not sure why, but the resource controller generation url test does not find the real 'Foos' url
        '/foo/123'|'foo'|'show'|'123'
    }
}

然后没有默认操作url $ controller / $ action / $ id(匹配foo/show/123),测试无法找到生成的URL。

生成的错误是:     junit.framework.AssertionFailedError:url'/ foo / 123'与任何映射都不匹配

有没有办法测试资源网址?

1 个答案:

答案 0 :(得分:0)

我知道这个问题已有5年之久了,但为将来参考,该问题可能与this grails-core Github issue (URLMapping unit test for RESTfull service is not working)中的问题相同。

this PR (Include request method in URL matching)显然已解决了该问题。看来此修补程序已合并到Grails 2.5.x中。