如何在 Scala REPL 中使用 Specs 匹配器?
在调试或探索规范功能时,在 REPL(Scala 解释器)中输入它们而不是在规范文件中输入并使用 Maven 之类的东西运行它会更有利。在 REPL 中创建与规范对象中相同的“环境”的最佳方法是什么?
更新: 看起来在 REPL 中试验规范匹配器的最简单方法是定义一些辅助子类并在其主体内使用表达式:
scala> class S extends Specification { override def toString = { reportSpecs; "" } }
defined class S
scala> new S { 1 mustEqual 2 }
Specification "anon"
x example 1
'1' is not equal to '2' (<console>:10)
Total for specification "anon":
Finished in 0 second, 4 ms
1 example, 1 expectation, 1 failure, 0 error
While debugging or exploring spec features it would be more advantageous to type them in REPL (Scala interpreter) rather then in file with spec and run it with something like maven. What is the optimal way to create in REPL the same "environment" as in Specification object?
Update:
It looks like the simplest way to experiment with specs' matchers in REPL is to define some helper subclass and use expressions inside its body:
scala> class S extends Specification { override def toString = { reportSpecs; "" } }
defined class S
scala> new S { 1 mustEqual 2 }
Specification "anon"
x example 1
'1' is not equal to '2' (<console>:10)
Total for specification "anon":
Finished in 0 second, 4 ms
1 example, 1 expectation, 1 failure, 0 error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 scala -classpath 启动 Scala 控制台,并为规范和您在规范中使用的其他库(例如 JUnit、Scalacheck)提供必要的 jar。或者,您可以使用 SBT 的控制台功能以正确的类路径启动控制台。
进入控制台后,您可以定义规范并执行它,如下所示。
您可能还想尝试 SBT 中的连续测试运行程序,它会在您每次保存 .scala 文件后自动重新编译并运行测试。从 SBT 控制台运行
> 〜测试
You can start the Scala console with
scala -classpath
and provide the neccesary jars for specs and other libraries you use from within specs (e.g. JUnit, Scalacheck). Alternatively, you could use the console feature from SBT to start the console with the correct classpath.Once in the console, you can define a spec and execute it, as below.
You may also like to try the continuous test runner in SBT, which automatically recompiles and runs tests after every time you save a .scala file. From the SBT console, run
> ~test
我不了解 Specs,但我已经使用 ScalaCheck 做到了这一点,它真正需要的只是将其 JAR 放在类路径中。
I don't know about Specs, but I have done so with ScalaCheck, and all it really needs is having its JAR in the classpath.