使用 spek 进行测试并分享 base-类 的一些基本测试用例

Testing with spek and sharing some base test-cases for base-classes

我使用 Spek 作为测试框架,在分享基础 classes 的一些测试步骤时遇到了麻烦。

我有一个抽象基础 class 和两个派生 classes.

abstract class Base {
    abstract fun send()
}
class Foo : Base() {
    override fun send() {}
    fun anotherFunction() { }
}
class Bar : Base() {
    override fun send() {}
    fun differentFunction() { }
}

现在我的问题是:如何为那些 classed 创建 Speks,但只在基本 spek 中为 send() 定义一次测试?

我的第一个方法是使用SubjectSpek

class BaseSpek : SubjectSpek<Base>({
    subject {
        // ??? Can't instantiate Base because it is abstract
    }

    it("test the base") { ... }
})
class FooSpek : SubjectSpek<Foo>({
    itBehavesLike(BaseSpek)

    it("test anotherFunction") { ... }
})

我的第二种方法是使用继承:

abstract class BaseSpek(base: Base) : Spek({
    it("test the base") { ... }
})
abstract class FooSpek() : BaseSpek(???)

看来我的 none 方法有效。任何建议如何解决这个问题?我是否应该提请 Spek 作者注意这一点,以便在未来的 Spek 版本中进行更改?

SubjectSpek才是正确的做法。

abstract class BaseSpec: SubjectSpek<Base>({
    it("test base") { ... }
})

object FooSpec: BaseSpec<Foo>({
    subject { ... }

    // ugly for now, until Spek supports @Ignore
    itBehavesLike(object: BaseSpec() {})

    it("test another") { ... }
})