【问题标题】:Test UrlMapping Paths for Resource Controllers测试资源控制器的 UrlMapping 路径
【发布时间】:2014-10-10 23:08:57
【问题描述】:

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

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

【问题讨论】:

    标签: grails grails-controller


    【解决方案1】:

    我知道这个问题已经超过 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 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多