【问题标题】:Run specs2 in sbt console在 sbt 控制台中运行 specs2
【发布时间】:2015-09-20 14:13:41
【问题描述】:

是否可以在 sbt 控制台中运行和试用 specs2?我试过 === 但这不起作用,因为我认为它是 Specification 类中的一种方法。我也试过了:

class A extends Specification{ "b" should{ "do" in{ 3 === 4 } } }; (new A).toResult(true) 

如果我知道正确的调用方法values,我怀疑上述方法应该有效吗?

如果我可以执行单个语句,理想情况下我会喜欢它,但如果那不可能,有没有办法运行这种块?

PS:specs2 有非 Fluent 接口吗?

【问题讨论】:

  • 什么叫“不流畅”的界面?
  • @Eric 感谢您的回答。不流利的意思是我想说 collection.hasSize(3) 而不是 collection must have size(3) ,只是我自己的一小部分,以便我可以更轻松地推断类型。顺便说一句,感谢伟大的框架。
  • 您可以“应用”任何匹配器:be_==(1)(2).toResult。不幸的是,对于集合,类型推断可能不会更好,具体取决于匹配器:haveSize[List[Int]](3).apply(List(1,2,3)).toResult(您需要注释)和contain(1)(List(1, 2)).toResult(您不需要)。此外,它读起来很奇怪......
  • 否则,如果您不想使用 must,您可以定义自己的扩展匹配器方法,但您必须为每个匹配器都这样做:implicit class traversableMatchers[I](c: Traversable[I]) { def hasSize(n: Int) = haveSize[Traversable[I]](n).apply(c) }

标签: scala specs2


【解决方案1】:

您可以执行以下操作以在控制台中运行期望:

import org.specs2._, matcher.MustMatchers._, execute._

def t[R:AsResult](r: =>R) = specs2.run(new Specification { def is = p^s2"test $r" })

// then
t(1 === 2)

[info] Specification
[info]
[error] x test
[error]  '1' is not equal to '2' (file:1)
[info]
[info] Total for specification Specification
[info] Finished in 5 ms
       1 example, 1 failure, 0 error
[info]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 2014-04-03
    • 2020-01-16
    • 2015-04-27
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    相关资源
    最近更新 更多