【问题标题】:ScalaTest own matcher, use of word notScalaTest自带匹配器,使用word not
【发布时间】:2013-12-02 20:45:38
【问题描述】:

所以一开始我有

def parseB(string : String)(implicit context : Context) : Float = parseAll(gexp, string).get.eval(context).left.get

然后在测试中

implicit var context = Context()
parseB("False") should be(false)
parseB("False") should not be(true)

然后我写了一个自定义匹配器

case class ReflectBooleanMatcher(value : Boolean)(implicit context : Context) extends Matcher[String] with ExpressionParser{
  def parseB(string : String) : Boolean = parseAll(gexp, string).get.eval(context).right.get
  def apply (string : String) : MatchResult = 
      MatchResult(parseB(string) == value, "", "")
}

所以我的测试变成了

"False" should reflectBoolean(false)

但是

"False" should not reflectBoolean(true)

Breaks- 当然,我从来没有说过它可以匹配负数。那我怎么说呢?

【问题讨论】:

    标签: scala scalatest


    【解决方案1】:

    你不需要改变你的匹配器,我认为你只需要一些括号:

    "False" should ReflectBooleanMatcher(false)
    "False" should not(ReflectBooleanMatcher(true)) //works!
    

    更新

    如果您将reflectBoolean 定义为:根据@Vidya 的cmets:

    def reflectBoolean(value: Boolean) = new ReflectBooleanMatcher(value)
    

    然后你可以使用'BDD'风格的语法,括号使这个工作:

    "False" should reflectBoolean(false)
    "False" should not(reflectBoolean(true))
    

    【讨论】:

    • 听起来你已经测试过了,所以如果它有效,它就有效。尽管如此,像这样使用类名ReflectBooleanMatcher 确实违反了BDD 的可读性,因为它消除了任何类型的DSL。那为什么还要使用 ScalaTest?
    【解决方案2】:

    诀窍是声明一个隐式类以将“反射”视为ResultOfNotWordForAny[String] 类型的 ("False"shouldnot) 结果的方法。

    def parseB(string : String)(implicit context : Context) : Float = parseAll(gexp, string).get.eval(context).left.get
    
    implicit var context = Context()
    parseB("False") should be(false)
    parseB("False") should not be(true)
    
    // Declares an implicit class for applying after not. Note the use of '!='
    implicit class ReflectShouldMatcher(s: ResultOfNotWordForAny[String]) {
      def reflect(value: Boolean) = s be(BeMatcher[String] {
        left => MatchResult(parseB(left) != value, "", "")
      })
    }
    // Declares an explicit method for applying after should. Note the use of '=='
    def reflect(right: Boolean) = Matcher[String]{ left =>
                MatchResult(parseB(left) == right, "", "")}
    
    // Now it works. Note that the second example can be written with or wo parentheses.
    "False" should reflect(false)
    "False" should not reflect true
    

    【讨论】:

      【解决方案3】:

      我只是要撕掉documentation

      trait CustomMatchers {    
        class ReflectBooleanMatcher(value: Boolean)(implicit context : Context) extends Matcher[String] with ExpressionParser {
           def parseB(string: String) : Boolean = parseAll(gexp, string).get.eval(context).right.get
           def apply(string: String) : MatchResult = MatchResult(parseB(string) == value, "", "")      
        }
      
        def reflectBoolean(value: Boolean) = new ReflectBooleanMatcher(value)
      }
      

      现在尝试使用reflectBoolean 方法。

      【讨论】:

        猜你喜欢
        • 2013-04-26
        • 2017-10-16
        • 2018-05-25
        • 2016-05-14
        • 2016-04-19
        • 2019-10-27
        • 1970-01-01
        • 2021-12-16
        • 1970-01-01
        相关资源
        最近更新 更多