在Around块中使用specs2匹配器

时间:2014-05-20 23:45:53

标签: scala specs2

如何从specs2中的Around块中的单独类的函数运行匹配器?

示例(使用Play框架):

class Spec extends Specification {
  "the application" should {
    "do something" in new WithBrowser { // <-- 1. because of this around block
      val runner = new Runner
      runner.check(browser)
    }
  }
}

class Runner extends MustMatchers {
  def check(browser: TestBrowser) = {
    browser.pageSource must contain("some text that isn't there") // <-- 2. this won't fail
  }
}

我在Around scaladoc中读到它没有与不会抛出FailureExceptions的匹配器一起工作。这是否意味着我必须亲自检查所有匹配器?我怎样才能让我的匹配器失败?

1 个答案:

答案 0 :(得分:2)

通过扩展MustMatchers,您遗憾地设法避免引入具有以下文档的所有重要MustThrownExpectations特征:

/**
* This trait provides implicit definitions to transform any value into a 
* MustExpectable, throwing exceptions when a match fails
*/

如果您从mutable.Specification一直遵循所有Specs2特征,那么有一个特征MustThrownMatchers,它会以“正确的方式”为您的用例失败;即:

class ThrowingRunner extends MustThrownMatchers {
  def check(browser: TestBrowser) = {
    browser.pageSource must contain("some text that isn't there")
  }
}