斯卡拉spec2 object.productID必须_ ==“123”失败
我已更新我的问题,删除旧文本,使其更容易阅读。斯卡拉spec2 object.productID必须_ ==“123”失败
scalaVersion := "2.10.1"
"org.specs2" %% "specs2" % "1.13" % "test"
我SPEC2测试:
package com.mycompany.dataminer.parser
import org.specs2.mutable.Specification
case class Product(productID:String)
class SimpleTest extends Specification {
"product" should {
"have id = 123" in {
var product1 = Product("123")
product1 must not beNull
product1.productID must_== "123"
var product2 = Product("123")
product2 must not beNull
product2.productID must_== "123"
var product3 = Product("123")
product3 must not beNull
product3.productID must_== "123"
}
}
}
结果:
scala: type mismatch;
found : String
required: org.specs2.matcher.Matcher[com.mycompany.dataminer.parser.Product]
product1.productID must_== "123"
^
有一次,我写了这个代码,这是工作,直到我添加此行:
product1 must not beNull
product2 must not beNull
product3 must not beNull
这是问题Scala的表达解析(半列推理)以及specs2模型匹配器的方式。
具有第一个匹配器product1 must not beNull
的行被解释为product1.must(not) beNull
。这意味着beNull
处于没有参数的方法调用的位置,但如果它有参数,它们必须是Matcher[Product]
。这是因为整个表达式的类型为MatchResult[Product]
,而MatchResult
特征的方法为apply
。
结果是Scala推断第一行表达式的参数位于第二行product1.productID
,并且类型String
这是意外的。
有这种情况3级的解决方法:
-
换行符
product1 must not beNull product1.productID must_== "123"
-
半柱
product1 must not beNull; product1.productID must_== "123"
-
一个parenthetised
not
product1 must not(beNull) product1.productID must_== "123"
这将在未来的版本specs2通过使MatchResult[T].apply
方法私人为了编译错误转化为method apply in trait MatchResult cannot be accessed in org.specs2.matcher.MatchResult[Product]
并在此方法中Scaladoc添加潜在问题的描述来缓解。
我的猜测:
- 导入名称冲突,或
- 什么不对劲的规格
也许下面的验收测试的例子可能提供了一个线索:
import org.specs2.Specification import xyz.{Product => TestProduct} // <- Alias the "Product" packagetrait MyTestHelper { val pId = "123" lazy val product = new TestProduct.Product(pId, ...) // <-- Class to test }
class MyTest extends Specification with MyTestHelper { def is = ("Product should be " + pId) ! product.ProductID must be_==("123")^ end }
我提供了新的说明。我想现在很容易重现。 – ses 2013-04-28 22:20:06
我已将此问题发布到spec2问题跟踪器(为了不忘记:)) – ses 2013-04-28 22:46:46