【问题标题】:Inheritance in Grails Spock Integration TestGrails Spock 集成测试中的继承
【发布时间】:2014-07-08 21:19:33
【问题描述】:

我的 Grails 集成测试套件(使用 Spock)中有一些非常相似的测试。我想要一个基础测试类,它包含测试的 90% 的通用逻辑,然后让测试类从它扩展。

我在想:

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() 适用于参数类型:() 值:[] 可能的解决方案:any()、old(java.lang.Object)、any(groovy.lang.Closure)、notify()、wait()、Spy() 在

有什么想法可以在我的 spock 集成测试中实现继承吗?

【问题讨论】:

  • 您需要在 SpecificClass 类声明中扩展 BaseClass 而不是 IntegrationSpec
  • @ShashankAgrawal 对不起,我正在这样做。我的错字。
  • 好的,那么您的代码本身的问题描述中有错字吗? (异常消失了吗?)
  • @ShashankAgrawal 谢谢。我更新了问题

标签: grails spock


【解决方案1】:

我不知道Spock能不能做到。当我尝试时,我找不到重用 spock 语句的方法,我所做的是编写一个 BaseSpecification 类,其中包含可在 spock 语句中使用的实用方法。

这是一个示例测试。

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

这是一个单元测试,但我认为它也应该与集成测试一起使用。

【讨论】:

  • 我认为这是你能得到的最接近的。如果有人可以提供更好的答案,将留下问题。问题是继承实际测试。但是当你想到你可能不应该这样做。
【解决方案2】:

好的,现在我明白你的意思了。

你几乎可以这样使用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 2013-10-19
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    相关资源
    最近更新 更多