如:
[error] C:\Users\Administrator\Desktop\scala\demo3\src\test\scala\DemoTest.scala
:12: could not find implicit value for evidence parameter of type org.specs2.exe
cute.AsResult[Any]
对于Specification中每个in等test结果都会产生一个值,例如你用mustEqual时就是一个boolean类型的结果,当是系统还会把给boolean转化成 AsResult[XXX]的值,具体是使用implicit的,而像boolean是有内置的implicit,即不用我们外加干涉,如果是其他的呢,最容易出现问题的是你使用match时会忽略
那么正确的做法就是自已提供implicit
如源码:
import org.specs2.execute.{Success, ResultExecution, AsResult}
import org.specs2.mutable.Specification
class WidgetApplySpec extends Specification {
implicit def unitAsResult: AsResult[Any] = new AsResult[Any] {
def asResult(r: =>Any) = ResultExecution.execute(r)(_ => Success())
}
"Widget.apply with a valid widget Specification String" should {
"""return a widget instance with the correct fields set""" in {
Widget("(button: label=click me, (Widget))") match{
case Some(w) => w match {
case Button(b) => b mustEqual "click me"
case x => println("this is ok, right?")
}
case _ => println("this is ok, right?")
}
}
}
}