2016-11-23 112 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 

为什么之间有差异和should比较字符串?

我使用SBT 0.13.8,斯卡拉2.12.0和specs2 3.8.6

+0

的可变规格的预期的结构是'should'(>''>>)>'in'和'必须'与匹配器 – cchantep

+0

您使用的是哪个版本的specs2?你可以用'3.8.6'来试试吗? – Eric

+0

与3.8.6版本相同 – obourgain

回答

0

的困难来自于should可以用来打开的实例块,但也描述了预期的事实。您可以通过以下特征的混合解决此

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

} 

,然后写你的规格一样

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

    "string comp" >> { 
    "first example" >> { 
     "abc" should beEqualTo("abc") 
    } 
    "second example" >> { 
     "def" should beEqualTo("def") 
    } 
    } 
}