问题描述
我正在尝试使用“ N个测试用例”编写一个测试,而不是为每个测试用例编写一个测试,我在Google上发现了Table
类,但是当我尝试将它们添加到我的测试中时类Table
不起作用,它不断给我错误:
无法解析重载的方法“表”
测试:
import org.scalatest.prop.TableDrivenPropertyChecks
class PublishEventServiceSpec extends AsyncWordSpec
with FixtureSupport
with Matchers
with AsyncmockFactory
with TableDrivenPropertyChecks {
"#my test cases" should {
"test cases" in {
val cases = Table(1,2)
forAll (cases) { c => assert(c != 0) }
}
}
}
方法forAll
也给我同样的错误“无法解析重载的方法'forAll'”,如何在测试中使用Table
和forAll
?
最原始版本:3.0.8
scala版本:2.11。*
解决方法
您快到了。这里的问题是Table首先使用String
类型的名称,然后可以添加行。这是您尝试使用的apply方法:
def apply[A](heading : _root_.scala.Predef.String,rows : A*) : org.scalatest.prop.TableFor1[A] = { /* compiled code */ }
定义时:
val cases = Table(1,2)
1
将不会转换为String
。为了解决这个问题,您需要添加一个名称,例如:
val cases = Table("name",1,2)
其余应相同。