繁体   English   中英

Grails 2.0.0模拟服务始终返回true吗?

[英]Grails 2.0.0 mock service always returns true?

我正在尝试模拟的控制器中有一个服务,但它始终返回true。 该服务如下所示:

def someService (x, y){...}

然后我在控制器测试中对其进行了嘲笑: mockservice.demand.someService () {-> return false }

它不断返回true。 我不知道怎么了 我尝试包括这些参数,但它仍未返回false。 这个怎么做?

PS:原谅错别字,我现在正在打电话。 谢谢

我假设您正在控制器中使用此服务,并且单元测试来自控制器。 我还假设您阅读了文档中的模拟协作者

@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()
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM