【发布时间】:2018-02-01 16:52:22
【问题描述】:
在 Grails 中,我的 UrlMappings 如下:
static mappings = {
'/route'(controller: 'route') {
action = [POST: 'save', GET: 'index']
}
}
我想为这些映射编写单元测试,但是我在documentation 中找不到如何使用 Http 方法 url 映射测试。
我尝试在断言中添加方法参数,但它不起作用
assertUrlMapping([controller: 'route', action: 'index', method: 'GET'], '/route')
assertUrlMapping([controller: 'route', action: 'save', method: 'POST'], '/route')
有什么办法吗?
编辑:
上面的第二个测试失败并显示junit.framework.ComparisonFailure: Url mapping action assertion for '/route' failed expected:<[save]> but was:<[index]> 消息。
主要问题是assertUrlMapping 似乎只适用于 GET 请求。
我已经通过将映射更改为:
static mappings = {
'/route'(controller: 'route') {
action = [POST: 'createRoute', PUT: 'updateRoute']
}
}
和测试:
assertUrlMapping([controller: 'route', action: 'updateRoute', method: 'PUT'], '/route')
assertUrlMapping([controller: 'route', action: 'createRoute', method: 'POST'], '/route')
失败并显示以下消息:
junit.framework.ComparisonFailure: Url mapping action assertion for '/route' failed expected:<[updateRoute]> but was:<[index]>
junit.framework.ComparisonFailure: Url mapping action assertion for '/route' failed expected:<[createRoute]> but was:<[index]>
【问题讨论】:
-
“它不起作用”是什么意思?
-
第二次测试失败,
junit.framework.ComparisonFailure: Url mapping action assertion for '/route' failed expected:<[save]> but was:<[index]>
标签: unit-testing grails url-mapping