测试资源控制器的UrlMapping路径



在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 

但是,在下面的文件中,无法测试url:

@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'不匹配任何映射

是否有测试资源url的方法?

我知道这个问题已经超过5年了,但作为将来的参考,这个问题可能与这个grails-core Github问题(RESTfull服务的URLMapping单元测试不起作用)相同。

这个问题显然是通过这个PR解决的(在URL匹配中包含请求方法)。看起来这个修复被合并到了Grails 2.5.x中。

最新更新