简体   繁体   English

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

[英]Grails 2.0.0 mock service always returns true?

I have a service in my controller I'm trying to mock but it always returns true. 我正在尝试模拟的控制器中有一个服务,但它始终返回true。 The service looks like this: 该服务如下所示:

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

Then I mocked it in my controller test: mockservice.demand.someService () {-> return false } 然后我在控制器测试中对其进行了嘲笑: mockservice.demand.someService () {-> return false }

It keeps returning true. 它不断返回true。 I don't know what's wrong. 我不知道怎么了 I tried including the parameters but its still not returning false. 我尝试包括这些参数,但它仍未返回false。 How to do this? 这个怎么做?

PS: Forgive the typos, I am on my phone right now. PS:原谅错别字,我现在正在打电话。 Thanks 谢谢

I'm assuming that you're using this service in an controller, and the unit test is from the controller. 我假设您正在控制器中使用此服务,并且单元测试来自控制器。 I'm assuming also that you read mocking collaborators in the docs. 我还假设您阅读了文档中的模拟协作者

@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