3

对于用 Java 编写的域对象,我有大量的 Hamcrest 匹配器。我现在正在迁移到 Scala,并希望在 specs2 测试的上下文中重用这些现有的匹配器。

给定 Foo 类的 Hamcrest 匹配器:

public class FooMatcher extends TypeSafeMatcher[Foo] {
...
}

我希望能够这样使用它:

val myFooMatcher = new FooMatcher(...)
foo must match (myFooMatcher)
foos must contain (myFooMatcher1, myFooMatcher2)

等等。

Specs2 似乎有相反的情况,它的 Matcher[T] 特征到 org.hamcrest.Matcher 的适配器,但我正在寻找相反的方式。

有任何想法吗?

4

1 回答 1

1

您需要添加一个隐式转换才能使其工作:

import org.hamcrest._
import org.specs2.matcher.MustMatchers._

implicit def asSpecs2Matcher[T](hamcrest: org.hamcrest.TypeSafeMatcher[T]):     
  org.specs2.matcher.Matcher[T] = {

  def koMessage(a: Any) = {
    val description = new StringDescription
    description.appendValue(a)
    hamcrest.describeTo(description)
    description.toString 
  }
  (t: T) => (hamcrest.matches(t), koMessage(t))  
}

让我们看看它的实际效果:

case class Foo(isOk: Boolean = true)

// a Hamcrest matcher for Foo elements
class FooMatcher extends TypeSafeMatcher[Foo] {
  def matchesSafely(item: Foo): Boolean    = item.isOk
  def describeTo(description: Description) = description.appendText(" is ko")
}

// an instance of that matcher
def beMatchingFoo = new FooMatcher

// this returns a success
Foo() must beMatchingFoo

// this returns a failure
Foo(isOk = false) must beMatchingFoo

// this is a way to test that some elements in a collection have
// the desired property
Seq(Foo()) must have oneElementLike { case i => i must beMatchingFoo }

// if you have several matchers you want to try out
Seq(Foo()) must have oneElementLike { case i => 
  i must beMatchingFoo and beMatchingBar 
}
于 2012-10-30T00:18:41.083 回答