2014-07-08 84 views
2

我有一些测试在我的Grails集成测试套件(它使用Spock)中非常相似。我想有一个基本的测试类,其中有90%的测试通用逻辑,然后让测试类从中延伸出来。Grails Spock集成测试中的继承

我在想:

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

    } 
} 

然后:

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

但是,试图为此,我得到两个问题:

  1. 它同时运行BaseClassSpecificationClass
  2. SpecificationClass点运行时,它失败上:

groovy.lang.MissingMethodException:方法的无签名:BaseSpecification.baseTest()是适用于参数类型:()值:()的任何:[] 可能的解决方案老(java.lang.Object中),任(groovy.lang.Closure),通知(),等待(),间谍() 在

任何想法如何,我可以在我的斯波克集成测试实现继承?

+0

您需要在** SpecificClass **类声明中扩展** BaseClass **而不是** IntegrationSpec **。 –

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

而且BaseSpecification类

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 
    } 
} 

这是一个单元测试,但我认为它应该具有的整合工作也进行测试。

+0

我认为这是最接近你可以得到的。如果有人能提供更好的答案,请留下问题。问题在于继承实际的测试。但是当你想起你时,不管怎样都不应该这样做。 –