我正在尝试让PlaySpec和Scalacheck一起很好地玩。我有一个看起来像这样的测试套件:
package controllers
import org.scalacheck.Prop.forAll
import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import org.scalatest.Matchers
import org.scalacheck.Gen
import org.scalatest.prop.GeneratorDrivenPropertyChecks
class Simple extends PlaySpec with OneAppPerSuite with GeneratorDrivenPropertyChecks {
"Simple suite" must {
"simple test" in forAll(Gen.choose(1, 2)) { i =>
i must be (1)
}
}
}
当Gen.choose限制指定为1、1时,就没有问题,并且测试通过。当我按上面的方法尝试1、2时,得到以下输出:
Simple:
[info] - play.api.libs.concurrent.ActorSystemProvider - Starting application default Akka system: application
[info] Simple suite
[info] - must simple test *** FAILED ***
[info] TestFailedException was thrown during property evaluation.
[info] Message: 2 was not equal to 1
[info] Location: (Simple.scala:14)
[info] Occurred when passed generated values (
[info] arg0 = 2
[info] )
Exception in thread "Thread-105" java.lang.ClassCastException: cannot assign instance of scala.collection.immutable.List$SerializationProxy to field org.scalatest.prop.Checkers$$anonfun$doCheck$3.scalaCheckArgs$2 of type scala.collection.immutable.List in instance of org.scalatest.prop.Checkers$$anonfun$doCheck$3
at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2133)
at java.io.ObjectStreamClass.setObjFieldValues(ObjectStreamClass.java:1305)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2006)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1924)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2000)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1924)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2000)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1924)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2000)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1924)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2000)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1924)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at org.scalatest.tools.Framework$ScalaTestRunner$Skeleton$1$React.react(Framework.scala:953)
at org.scalatest.tools.Framework$ScalaTestRunner$Skeleton$1.run(Framework.scala:942)
at java.lang.Thread.run(Thread.java:745)
Reporter completed abruptly with an exception after receiving event: TestFailed(Ordinal(0, 4),TestFailedException was thrown during property evaluation.
Message: 2 was not equal to 1
Location: (Simple.scala:14)
Occurred when passed generated values (
arg0 = 2
),Simple,controllers.Simple,Some(controllers.Simple),Simple suite must simple test,simple test,Vector(),Some(org.scalatest.exceptions.GeneratorDrivenPropertyCheckFailedException: TestFailedException was thrown during property evaluation.
Message: 2 was not equal to 1
Location: (Simple.scala:14)
Occurred when passed generated values (
arg0 = 2
)),Some(224),Some(IndentedText(- must simple test,must simple test,1)),Some(SeeStackDepthException),Some(controllers.Simple),None,pool-1-thread-1-ScalaTest-running-Simple,1455209757204).
java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1286)
at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1231)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1427)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1577)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351)
at org.scalatest.tools.SocketReporter.apply(SocketReporter.scala:31)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$1.apply(DispatchReporter.scala:240)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$1.apply(DispatchReporter.scala:239)
at scala.collection.immutable.List.foreach(List.scala:381)
at org.scalatest.DispatchReporter$Propagator.run(DispatchReporter.scala:239)
at java.lang.Thread.run(Thread.java:745)
Reporter completed abruptly with an exception after receiving event: ScopeClosed(Ordinal(0, 5),Simple suite,NameInfo(Simple,controllers.Simple,Some(controllers.Simple),None),Some(MotionToSuppress),Some(LineInFile(11,Simple.scala)),None,pool-1-thread-1-ScalaTest-running-Simple,1455209757265).
java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1286)
at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1231)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1427)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1577)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351)
at org.scalatest.tools.SocketReporter.apply(SocketReporter.scala:31)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$1.apply(DispatchReporter.scala:240)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$1.apply(DispatchReporter.scala:239)
at scala.collection.immutable.List.foreach(List.scala:381)
at org.scalatest.DispatchReporter$Propagator.run(DispatchReporter.scala:239)
at java.lang.Thread.run(Thread.java:745)
[info] - play.api.libs.concurrent.ActorSystemProvider - Shutdown application default Akka system: application
Reporter completed abruptly with an exception after receiving event: SuiteCompleted(Ordinal(0, 6),Simple,controllers.Simple,Some(controllers.Simple),Some(3374),Some(MotionToSuppress),Some(TopOfClass(controllers.Simple)),None,None,pool-1-thread-1,1455209757376).
java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1286)
at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1231)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1427)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1577)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351)
at org.scalatest.tools.SocketReporter.apply(SocketReporter.scala:31)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$1.apply(DispatchReporter.scala:240)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$1.apply(DispatchReporter.scala:239)
at scala.collection.immutable.List.foreach(List.scala:381)
at org.scalatest.DispatchReporter$Propagator.run(DispatchReporter.scala:239)
at java.lang.Thread.run(Thread.java:745)
Reporter completed abruptly with an exception after receiving event: RunCompleted(Ordinal(0, 7),Some(3870),Some(Summary(0,1,0,0,0,1,0,0)),None,None,None,main,1455209757409).
java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1286)
at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1231)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1427)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1577)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351)
at org.scalatest.tools.SocketReporter.apply(SocketReporter.scala:31)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$1.apply(DispatchReporter.scala:240)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$1.apply(DispatchReporter.scala:239)
at scala.collection.immutable.List.foreach(List.scala:381)
at org.scalatest.DispatchReporter$Propagator.run(DispatchReporter.scala:239)
at java.lang.Thread.run(Thread.java:745)
Reporter completed abruptly with an exception on invocation of the dispose method.
java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
at java.io.ObjectOutputStream$BlockDataOutputStream.flush(ObjectOutputStream.java:1822)
at java.io.ObjectOutputStream.flush(ObjectOutputStream.java:719)
at org.scalatest.tools.SocketReporter.dispose(SocketReporter.scala:37)
at org.scalatest.DispatchReporter$.propagateDispose(DispatchReporter.scala:312)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$2.apply(DispatchReporter.scala:251)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$2.apply(DispatchReporter.scala:250)
at scala.collection.immutable.List.foreach(List.scala:381)
at org.scalatest.DispatchReporter$Propagator.run(DispatchReporter.scala:250)
at java.lang.Thread.run(Thread.java:745)
[info] ScalaTest
[info] Run completed in 4 seconds, 947 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 0, aborted 0
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
[info] No tests were executed.
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error] controllers.Simple
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 7 s, completed 11-Feb-2016 16:55:57
我是Scala的新手,所以我可能错了,但是似乎forAll的输出不是'in'函数应该返回的内容。如果是这种情况,我应该如何包装测试以符合PlaySpec?
感谢任何帮助。
谢谢!
例如...
class ShortenerControllerEssentialSpec
extends PlaySpec
with GuiceOneAppPerSuite
with PrivateMethodTester
with ScalaCheckDrivenPropertyChecks {
//...
"shortener from given an Action[ShortenerModel]" must {
"returns the status code 201" in {
forAll { url: String =>
val request: FakeRequest[ShortenerModel] = FakeRequest()
.withHeaders("Content-Type" -> "application/json")
.withBody(ShortenerModel(genCorrectUrl(url)))
val result = controller.shortener().apply(request)
implicit val timeout: Timeout = Timeout(5 seconds)
status(result) mustEqual 201
}
}
"returns the status code 400" in {
forAll { url: String =>
val request: FakeRequest[ShortenerModel] = FakeRequest()
.withHeaders("Content-Type" -> "application/json")
.withBody(ShortenerModel(url))
val result = controller.shortener().apply(request)
implicit val timeout: Timeout = Timeout(5 seconds)
status(result) mustEqual 400
}
}
}
...