How to play with Specs matchers in Scala REPL?
While debugging or exploring spec features it would be more advantageous to type them in REPL (Scala i开发者_StackOverflownterpreter) 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
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.
Welcome to Scala version 2.8.0.Beta1-RC5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_15).
Type in expressions to have them evaluated.
Type :help for more information.
scala> object Foo extends org.specs.Specification {
| "1 + 1" in { (1 + 1) must_== 2 }
| }
defined module Foo
scala> Foo.reportSpecs
Specification "Foo"
+ 1 + 1
Total for specification "Foo":
Finished in 0 second, 184 ms
1 example, 1 expectation, 0 failure, 0 error
res0: Foo.type = Foo
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
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.
精彩评论