Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala spec2 I am Unable to create a test that uses must be_== and failure at the same time

I am new to Scala and Spec2.

I would like to create the following test but I get an error from the compiler.

Here is the test I would like to write

import org.specs2.mutable._
import org.specs2.specification._
import org.specs2.matcher._
import org.specs2.matcher.MatchResult

class SimpleParserSpec extends Specification {

"SimpleParser" should {

val parser = new SimpleParser()

  "work with basic tweet" in {
      val tweet = """{"id":1,"text":"foo"}"""
      parser.parse(tweet) match {
        case Some(parsed) => {
                                parsed.text must be_==("foo")
                                parsed.id must be_==(1)
                              }
        case _ =>  failure("didn't parse tweet") 
      }
    }
  }
}

I get the error: C:\Users\haques\Documents\workspace\SBT\jsonParser\src\test\scala\com\twitter\sample\simpleSimpleParserSpec.scala:17: could not find implicit value for evidence parameter of type org.specs2.execute.AsResult[Object]

Regards,

Shohidul

like image 559
shodz Avatar asked Aug 13 '14 15:08

shodz


2 Answers

The compiler produces an error here because he tries to unify a MatchResult[Option[Parsed]] with a failure of type Result. They unify as Object and the compiler can't find an AsResult typeclass instance for that. You can fix your example by providing another MatchResult for the failed case:

parser.parse(tweet) match {
  case Some(parsed) => {
    parsed.text must be_==("foo")
    parsed.id must be_==(1)
  }
  case _ =>  ko("didn't parse tweet")
}

The ok and ko methods are the equivalent of success and failure but are MatchResults instead of being Results.

like image 100
Eric Avatar answered Nov 05 '22 01:11

Eric


Would better write it as following:

"work with basic tweet" in {
  val tweet = """{"id":1,"text":"foo"}"""
  parser.parse(tweet) aka "parsed value" must beSome.which {
    case parsed => parsed.text must_== "foo" and (
      parsed.id must_== 1)
  }
}
like image 42
cchantep Avatar answered Nov 05 '22 03:11

cchantep