8

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

4

3 に答える 3

8

コンパイラは、MatchResult[Option[Parsed]]タイプ の失敗でa を統合しようとするため、ここでエラーを生成しますResult。それらは as として統合されObject、コンパイラはその型クラスのAsResultインスタンスを見つけることができません。MatchResult失敗したケースに別の例を提供することで、例を修正できます。

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

okandkoメソッドは and と同等ですが、 ではsuccessありfailureませMatchResultsResults

于 2014-08-14T00:35:38.610 に答える
2

次のように書いたほうがよいでしょう。

"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)
  }
}
于 2014-08-13T16:22:26.563 に答える
0

あなたが試すことができることの 1 つは、SimpleParser をトレイトにすることです。これは、Specs2 を使用している場合、通常はうまく機能します。次に、parse(tweet) を呼び出すことができます。また、テストを少し分割することをお勧めします。

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
         }
}

ここから、テストしたい他のフィールドを追加できます。

編集:私が書いたことを振り返ってみると、あなたが求めていたことに完全に答えていないことがわかります. === を入れてから、すでに持っているようなケースに失敗することができますが、私が持っているものの枠組みの中で。

于 2014-08-13T16:29:22.643 に答える