我在我的控制器中有一个服务我试图模拟,但它总是返回true。该服务如下所示:
def someService (x, y){...}
然后我在我的控制器测试中嘲笑它:
mockservice.demand.someService () {-> return false }
它一直回归真实。我不知道什么是错的。我尝试包含参数,但仍然没有返回false。这该怎么做?
PS:原谅错别字,我现在正打电话。感谢答案 0 :(得分:0)
我假设您在控制器中使用此服务,并且单元测试来自控制器。我还假设您在文档中阅读了mocking collaborators。
@TestFor(MyController)
class MyControllerSpec {
def setup() {
def mockServiceControl = mockFor(SomeService)
//here you limit the amount of times that the method should be called.
mockServiceControl.demand.someService(0..1) { x, y ->
return false
}
//probably you're missing to assign the attribute in the controller
controller.someService = mockServiceControl.createMock()
}
void testSomething() {
controller.action()
//assert response
//Then verify that the demand mocking is correct
mockServiceControl.verify()
}
}