请原谅,如果这是一个n00b问题,我是一个n00b grails ...
我已经安装了"rest"插件grails install-plugin rest.
我的服务类有这个代码(编辑):
def index() {
def data
withRest(uri:'http://localhost:8090/some/valid/url/running/here/') {
auth.basic 'admin', 'admin'
def response = get(path: 'something', query: [format: 'json'])
data = response.data
}
return data
}
Run Code Online (Sandbox Code Playgroud)
如果我运行grails console,实例化我的服务类并调用service.index(),我得到我预期的JSON结果.此代码按预期工作.它通过控制器工作.它甚至可以通过集成测试通过控制器工作.
这是我的单元测试:
void testIndex() {
def response = service.index()
assertEquals(response.total, 2)
assertEquals(response.receipts.size, 2)
assertEquals(response.receipts.collectEntries{ [it.id, [id: it.id]] }, [1: [id:1], 2:[id:2]])
}
Run Code Online (Sandbox Code Playgroud)
这失败并出现错误:
groovy.lang.MissingMethodException: No signature of method: torch.ReceiptService.withRest() is applicable for argument types: (java.util.LinkedHashMap, torch.ReceiptService$_index_closure1) values: [[uri:http://localhost:8090/some/valid/url/running/here/], ...]
Run Code Online (Sandbox Code Playgroud)
因此,当测试运行时,插件似乎不活动.我没有对插件做任何其他配置.我真的不明白为什么测试环境应该以不同的方式编译这个类.
我的目的是在我实现此目的后模拟网络接口,因为它具有外部依赖性.但我一步一步迈出了一步.
我是否需要注入模拟withRest()甚至运行测试?或者是其他不妥之处?
谢谢!
这就是单元测试的工作原理.没有插件是活动的,没有Spring,Hibernate等.你只是在运行一个类,所以一切都必须被嘲笑.看起来像REST是一个不好的嘲弄候选人,从那以后你只是在测试嘲笑.
我可能会通过功能测试来测试它.遗憾的是,这不如单元测试方便,但是糟糕的测试并不是很有用:)你可以配置它以便在配置中查找url以允许不同的URL进行测试,这样你就不需要点击外部服务了,然后设置一个真正的REST服务进行测试,返回已知值.
| 归档时间: |
|
| 查看次数: |
848 次 |
| 最近记录: |