Scala spec2 I cannot create a test that should be _ == and crash 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 a 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 an implicit value for the proof parameter of type org.specs2. execute.AsResult [Object]

Hi,

Shohidul

+7
scala testing specs2
source share
3 answers

The compiler generates an error here because it is trying to combine MatchResult[Option[Parsed]] with a failure of type Result . They are combined as an Object , and the compiler cannot find an instance of type AsResult typeclass for this. You can fix your example by providing another MatchResult for a failed event:

 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 equivalent to success and failure , but are MatchResults instead of Results .

+7
source share

It is better to write it as follows:

 "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) } } 
+2
source share

one thing you could try is to make SimpleParser a trait. This usually works better for me when using Specs2. Then you can call parsing (tweet). I also suggest revealing the tests a bit.

 class SimpleParserSpec extends Specification with SimpleParser { val tweet = """{"id":1,"text":"foo"}""" SimpleParser should { "parse out the id" in { val parsedTweet = parse(tweet) parsedTweet.id === 1 } } 

Here you can add other fields that you want to check.

EDIT: Looking back at what I wrote, I see that I have not fully answered what you asked. you could put === and then crash in cases like you already, but within the scope of what I have.

0
source share

All Articles