2016-11-23 10 views
2

specs2を使用していますが、mustshouldは同等です(What is the difference between should and must in scala testing?参照)。どちらか一方を使用すると個人的な好みに過ぎません。Specs2文字列の比較が正しく動作しない

しかし、must作品使用して文字列、次のテストを比較するとき:

import org.specs2.mutable._ 

class StringEqualWithMust extends Specification { 

    "string comp " should { 
    "abc" must beEqualTo("abc") 
    } 
} 

をしかしshouldを使用して、同じテストはコンパイルされません。

import org.specs2.mutable._ 

class StringEqualWithShould extends Specification { 

    "string comp " should { 
    "abc" should beEqualTo("abc") 
    } 
} 

をコンパイルエラーがある:

StringEqualWithShould.scala:7: overloaded method value should with alternatives: 
[error] (fs: => org.specs2.specification.core.Fragments)(implicit p1: org.specs2.control.ImplicitParameters.ImplicitParam1)org.specs2.specification.core.Fragments <and> 
[error] (f: => org.specs2.specification.core.Fragment)org.specs2.specification.core.Fragment 
[error] cannot be applied to (org.specs2.matcher.BeEqualTo) 
[error]  "abc" should beEqualTo("abc") 
[error]   ^
[error] one error found 

なぜmustshould文字列を比較すると?

私は難易度がshouldは例のブロックを開くことなく、期待を記述するために使用することができるという事実から来ているのsbt 0.13.8、スカラ座2.12.0、およびspecs2 3.8.6

+0

のようなあなたの仕様を記述することができますmatcherを使って 'must 'と入力してください – cchantep

+0

どのバージョンのspecs2を使用していますか?あなたは '3.8.6'で試してみることができますか? – Eric

+0

バージョン3.8.6の場合と同じ結果 – obourgain

答えて

0

を使用しています。あなたは、次の特性

import org.specs2.specification.core._ 
import org.specs2.control.ImplicitParameters._ 
import org.specs2.specification.dsl.mutable.ExampleDsl 

trait NoShouldBlock extends ExampleDsl { 

    override def describe(s: String) = super.describe(s) 

    implicit class block(d: String) { 
    def >>(f: => Fragment): Fragment = describe(d).>>(f) 
    def >>(fs: => Fragments)(implicit p1: ImplicitParam1): Fragments =  describe(d).>>(fs)(p1) 
    } 

} 

に混合することによってこの問題を回避そして、変更可能な仕様のため、予想される構造は、 `should`(>` `>>)>` in`ある

class StringEqualWithShould extends org.specs2.mutable.Specification with NoShouldBlock { 

    "string comp" >> { 
    "first example" >> { 
     "abc" should beEqualTo("abc") 
    } 
    "second example" >> { 
     "def" should beEqualTo("def") 
    } 
    } 
} 
関連する問題