-1

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

我有一个抽象基类和两个派生类。

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

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

我的第一种方法是使用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(???)

看来我的方法都不起作用。任何建议如何解决这个问题?我是否应该提请 Spek-Author 关注 Spek 未来版本中可能发生的变化?

4

1 回答 1

1

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") { ... }
})
于 2017-10-04T10:26:19.007 回答