2012-07-28 2 views
3

서비스 단위 테스트에서 조롱 된 서비스의 속성을 설정하는 방법을 알아낼 수 없습니다. 나는 수요 객체와 오류가 그것을 필요로 내가 테스트 해요 방법 내에서 발생, No such property: propToSet for class: grails.test.GrailsMock이며 이들 중 대부분 Grails의 2grails.test.GrailsMock에 대한 속성 설정

@TestFor(SomeService) 
@Mock([HelperService]) 
class SomeServiceTests { 

void testDoSomething() { 
    def helperService = mockFor HelperService 

    // tried this, error in method being tested 
    helperService.setProperty('propToSet',['a','b']) 
    // tried this, error in test 
    helperService.demand.propToSet = ['a','b'] 
    // tried this, error in method being tested 
    helperService.demand.getPropToSet() {['a','b']} 

    service.helperService = helperService 

    assert service.doSomething('aa') != null 
} 
} 

에서 사라질 것 같습니다 setProperty 방법을 사용하여 시도했습니다. 위의 두 번째 옵션은 실제로 어려운 오류를 제공합니다. 내가 조롱 된 Grails 객체에서 속성을 설정하는 방법은 무엇입니까?

답변

0

나는 또한 Grails 조롱 설비에 대한 그다지 좋은 경험이 없다. 그래서 저는 GMock을 사용해 왔고 행복했습니다. GMock은 컨트롤러, 서비스 및 도메인 클래스뿐만 아니라 Spock의 사양을 포함한 모든 Grails 테스트와 잘 작동합니다. 그것을 사용하려면

, 당신은 단순히 관례에 따라 grails-app/conf/BuildConfig.groovy에 다음 줄을 넣어 :

dependencies { 
    test 'org.gmock:gmock:0.8.2' 
} 

을 그리고 이것은 코드의 GMock 버전입니다. 당신의 모의 코드는 것

@WithGMock 
@TestFor(SomeService) 
class SomeServiceTests { 

    void testDoSomething() { 
     def helperService = mock(HelperService) 
     helperService.propToSet.returns(['a', 'b'])  
     service.helperService = helperService 
     play { 
      assert service.doSomething('aa') != null 
     } 
    } 

} 

참고 만 play { } 블록에 영향을줍니다. 그래서 assert 문을 감싸는 블록이 필요합니다.

+0

이 내용을 확인하고 진행 상황을 알려야합니다. –