2014-07-08 8 views
2

Grails 통합 테스트 스위트 (Spock을 사용)에서 매우 유사한 몇 가지 테스트가 있습니다. 나는 테스트의 공통 논리의 90 %를 가지고있는 기본 테스트 클래스를 갖고 싶어하고 테스트 클래스가 그것으로부터 확장되도록한다.Grails Spock 통합 테스트에서의 상속

나는 생각했다 :

  1. 그것은 모두 BaseClassSpecificationClass
  2. 을 실행 : 나는 두 가지 문제를 얻을 수

    public class SpecificTestSpecification extends BaseSpecification { 
        public baseTest() { 
         setup: 
          // more set up 
          super.baseTest(); 
         when: 
          // some more specific testing 
         then: 
          // som more testing 
        } 
    } 
    

    그러나이 시도 : 다음

    public abstract BaseSpecification extends IntegrationSpec { 
        public baseTest() { 
         // 
         setUp: 
         // 
         ... 
         when: 
         // 
         ... 
         then: 
         ...  
    
        } 
    } 
    

groovy.lang.MissingMethodException :210 실행, 그것은 실패하지 방법의 어떠한 서명 : [] 가능한 솔루션 : 하나를()() 값 : BaseSpecification.baseTest()는 인수 유형에 적용 오래된 (java.lang.Object 상위), 어떤 (groovy.lang.Closure), 내 스팍 통합 테스트에서 상속을 달성 할 수있는 방법

어떤 아이디어에서) (기다려) (스파이() 통지 ?

+0

** SpecificClass ** 클래스 선언에서 ** IntegrationSpec ** 대신 ** BaseClass **를 확장해야합니다. –

+0

@ShashankAgrawal 죄송합니다. 내 잘못 입력. –

+0

자, 코드 자체에 문제 설명에 오타가있었습니다. (예외가 있습니까?) –

답변

0

이제 알았습니다.

할 수 있습니다이 같은 꽤 많이 사용 :

class BaseSpecification extends IntegrationSpec { 

    //User userInstance 

    def setup() { 
     // do your common stuff here like initialize a common user which is used everywhere 
    } 

    def cleanup() { 
    } 
} 

class SpecificTestSpecification extends BaseSpecification { 

    def setup() { 
     // specific setup here. Will call the super setup automatically 
    } 

    def cleanup() { 
    } 

    void "test something now"() { 
     // You can use that userInstance from super class here if defined. 
    } 
} 
1

가 스팍 함께 할 수 있을지 모르겠어요. 내가 시도했을 때 spock 문을 재사용 할 수있는 방법을 찾지 못했고 spock 문 안에서 사용할 수있는 유틸리티 메서드로 BaseSpecification 클래스를 작성했습니다.

이것은 예제 테스트입니다.

@TestFor(Address) 
class AddressSpec extends BaseSpecification { 
... 
    void "Country code should be 3 chars length"(){ 

     when: 
      domain.countryCode = countryCode 

     then: 
      validateField('countryCode', isValid, 'minSize.notmet') 

     where: 
      [countryCode, isValid] << getMinSizeParams(3) 
    } 

그리고 클래스

class BaseSpecification extends Specification { 

    // Return params that can be asigned in `where` statement 
    def getMinSizeParams(Integer size){[ 
     [RandomStringUtils.randomAlphabetic(size - 1), false], 
     [RandomStringUtils.randomAlphabetic(size),  true] 
    ]} 

    // Make an assetion, so it can be used inside `then` statement 
    protected void validateField(String field, String code, Boolean shouldBeValid){ 
     domain.validate([field]) 
     if(shouldBeValid) 
      assert domain.errors[field]?.code != code 
     else 
      assert domain.errors[field]?.code == code 
    } 
} 

이는 단위 테스트입니다하지만 난 그게 통합 작업을해야한다고 생각이 너무 테스트하는 BaseSpecification.

+0

나는 이것이 당신이 얻을 수있는 가장 가까운 것이라고 생각한다. 누군가가 더 나은 답변을 제공 할 수 있도록 질문을 남깁니다. 문제는 실제 테스트를 이어받습니다. 그러나 당신에 대해 생각할 때 어쨌든 이것을해서는 안됩니다. –

관련 문제