2016-08-26 7 views
1

핸들러가 포함 된 체인 관리자가 있습니다. 그것은 기본적으로 특정 클래스의 처리기가 있는지 확인하고 싶습니다. 그렇게하는 방법?어설 션 목록에 Spock의 주어진 클래스 요소가 포함되어 있습니다.

def "contains few updaters on default"(){ 
    when: 
     def manager = new UpdateManager(); 
    then: 
     manager.getUpdaters().size() == 2; 
     //how to check that the list contains elements of classes 
     //OneThingUpdater and OtherThingUpdater 
} 

편집 : 나는 같은 클래스의 두 가지 요소에 대해 방어하기 위해, 대답의 코드를 개선했습니다, 그래서 지금이 같다 :

def "contains few updaters on default"(){ 
    setup: 
     def expectedUpdaters = [OneThingUpdater, OtherThingUpdater] 
    when: 
     def manager = new UpdateManager(); 

    then: 
     def list = manager.getUpdaters() 
     list.size() == 2; 
     list.every { 
      it.class in expectedUpdaters 
      expectedUpdaters.remove(it.class) 
     } 
} 

답변

3
def "contains few updaters on default"(){ 
    when: 
    def manager = new UpdateManager() 

    then: 
    def list = manager.getUpdaters() 
    list.size() == 2 
    list*.getClass().every { it in [OneThingUpdater, OtherThingUpdater] } 

    // or, just this 
    // Assuming the classes do not implement Map interface 
    // getClass() can be replaced with class 
    list*.class == [OneThingUpdater, OtherThingUpdater] 
} 
+0

이 경우 전달합니다 우리 두 명의 OneThingUpdaters가 있지만 영감을 가져 주셔서 감사합니다. 예상 한 구독자 목록을 "when"에 포함하도록 변경했으며 발견 된 모든 요소에 대해 예상 목록에서 제거했습니다. – Qbix

+0

@Qbix 업데이트 된 답변을 확인하십시오. – dmahapatro