【发布时间】:2016-12-15 01:30:20
【问题描述】:
ScalaTest WordSpec 允许像这样忽略测试:
class MySpec extends WordSpec {
"spec" should {
"ignore test" ignore {fail("test should not have run!")}
}
}
这很好,但我不想忘记忽略的测试。所以我希望忽略行为在提供的日期之后过期。此时测试将正常运行,并且:1) 通过(希望如此)或 2) 提醒我它仍然损坏。
为了实现这一点,我正在尝试扩展 WordSpec DSL 以支持 ignoreUntil 函数。这将接受一个字符串到期日期,如果该日期仍在未来,则忽略测试,否则运行测试。
我的测试规范将如下所示:
class MySpec extends EnhancedWordSpec {
"spec" should {
"conditionally ignore test" ignoreUntil("2099-12-31") {fail("test should not have run until the next century!")}
}
}
我在这里实现了ignoreUntil 函数:
class EnhancedWordSpec extends WordSpecLike {
implicit protected def convertToIgnoreUntilWrapper(s: String) = new IgnoreUntilWordSpecStringWrapper(s)
protected final class IgnoreUntilWordSpecStringWrapper(wrapped: String) {
// Run test or ignore, depending if expiryDate is in the future
def ignoreUntil(expiryDate: String)(test: => Any): Unit = ???
}
}
但是sbt test 给了我以下编译错误:
MySpec.scala:3: type mismatch;
[error] found : Char
[error] required: String
[error] "ignoreUntil" ignoreUntil("2099-12-31"){fail("ignoreUntil should not have run!")}
[error] ^
[error] one error found
[error] (test:compileIncremental) Compilation failed
为什么编译器不喜欢ignoreUntil函数的签名?
是否存在一些隐含的巫术?
【问题讨论】: