2014-04-04 2 views
0

내 컨트롤러에 서비스가 있습니다. 나는 조롱하려고 노력하고 있지만 항상 사실을 반환합니다. 이 서비스는 다음과 같습니다 mockservice.demand.someService() {-> return false }Grails 2.0.0 모의 서비스는 항상 true를 반환합니까?

그것은 반환 계속 사실 :

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

가 그럼 난 내 컨트롤러 테스트에 조롱. 나는 틀린 것을 모른다. 매개 변수를 포함 시키려고했으나 여전히 false를 반환하지 않습니다. 이 작업을 수행하는 방법?

추신 : 오타를 용서하십시오, 나는 지금 내 전화에 있습니다. 감사합니다

+0

조롱 중에 매개 변수가 일치해야합니다. – dmahapatro

답변

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() 
    } 
} 
+0

mockFor()는 mockControl을 제공합니다. 조롱 된 객체를 얻으려면'createMock()'을 호출해야한다. – dmahapatro

+0

누락 된 부분을 가져 주셔서 감사합니다. 내가 게으른 느낌 :-) –

관련 문제