资源控制器的测试URL映射路径

问题描述 投票:4回答:1

在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的方法?

grails grails-controller
1个回答
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中。

© www.soinside.com 2019 - 2024. All rights reserved.