标签: specs2

使用specs2进行seq空测试

如何Seq[String]在Scala中使用specs2 检查a 是否为空?我正在使用seq must be emptyseq.length must be greaterThan(0)但最终总是出现类型不匹配错误.

ret is Seq[String]

ret.length must be greaterThan(0)

[error] ApiTest.scala:99: type mismatch;
[error]  found   : Int
[error]  required: org.specs2.matcher.Matcher[String]
[error]         ret.length must be greaterThan(0)
Run Code Online (Sandbox Code Playgroud)

unit-testing specs scala specs2

6
推荐指数
2
解决办法
3059
查看次数

播放框架2:使用json字符串作为正文测试请求

我有以下行动

def save() = Action(parse.json) { implicit request =>
  request.body.asOpt[IdeaType].map { ideatype =>
    ideatype.save.fold(
      errors => JsonBadRequest(errors),
      ideatype => Ok(toJson(ideatype))
    )
  }.getOrElse     (JsonBadRequest("Invalid type of idea entity"))
}
Run Code Online (Sandbox Code Playgroud)

我想测试一下

Web服务正常运行,如下所示:

curl -X post "http://localhost:9000/api/types" 
--data "{\"name\": \"new name\", \"description\": \"new description\"}" 
--header "Content-type: application/json"
Run Code Online (Sandbox Code Playgroud)

正确返回新资源

{"url":"/api/types/9","id":9,"name":"new name","description":"new description"}
Run Code Online (Sandbox Code Playgroud)

我正试着用它来测试它

"add a new ideaType, using route POST /api/types" in {
  running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {

    val json = """{"name": "new name", "description": "new description"}"""

    val Some(result) = routeAndCall(
      FakeRequest(
        POST, 
        "/api/types",
        FakeHeaders(Map("Content-Type" …
Run Code Online (Sandbox Code Playgroud)

testing scala specs2 playframework-2.0

6
推荐指数
1
解决办法
7921
查看次数

Specs2和@ Before/@ After-like方法

鉴于代码如下:

class RESTAcceptanceTest extends Specification {

  override def is = anonymous ^ signinOAuth

  def anonymous = "Something"       ^ givenSomething ^
                  "must happen"     ^ mustHappen 
  end

  def signinOAuth = "Signin"                ^ givenSignin ^
                    "works as expected"     ^ userExistsInDatabase 
  end

  // rest of object definitions

}
Run Code Online (Sandbox Code Playgroud)

如何确保在"anonymous" "signinOAuth" 之前/之后执行相同的代码之前和之后,即使测试失败,"after"方法也应该执行?

scala specs2

6
推荐指数
1
解决办法
4689
查看次数

播放模块测试和FakeApplication

我想知道在PlayFramework模块上运行specs2测试的最佳方法是什么,并能够模拟它运行.

我的模块在名为mymodule.routes的文件中包含一些路由在我的应用程序中,我通过在路由文件中添加以下行来集成它们

->  /mymodule mymodule.Routes
Run Code Online (Sandbox Code Playgroud)

这是我尝试运行的模块测试,但返回404错误:

"test myroute" in {
  running(FakeApplication()) {
    await(WS.url("http://localhost:9000/mymodule/myroute").get).status must equalTo(OK)
  }
}
Run Code Online (Sandbox Code Playgroud)

scala module specs2 playframework-2.0

6
推荐指数
1
解决办法
1068
查看次数

使用Akka TestKit和Specs2

我正在尝试使用Akka的TestKit进行specs2测试.我坚持一个持续的编译错误我无法弄清楚如何解决,我很感激建议.

编译错误是:

TaskSpec.scala:40: parents of traits may not have parameters
[error]   with akka.testkit.TestKit( ActorSystem( "testsystem", ConfigFactory.parseString( TaskSpec.config ) ) )
Run Code Online (Sandbox Code Playgroud)

根据Akka docs和internet xebia以及Akka in Action的建议,我试图将TestKit合并到specs2 Scope中.这是我得到错误的代码片段:

class TaskSpec 
extends Specification 
with AsyncTest
with NoTimeConversions { 

  sequential 

  trait scope 
  extends Scope 
  with TestKit( ActorSystem( "testsystem", ConfigFactory.parseString( TaskSpec.config ) ) ) 
  with AkkaTestSupport {
...
Run Code Online (Sandbox Code Playgroud)

我有以下帮手:

trait AkkaTestSupport extends After { outer: TestKit =>
  override protected def after: Unit = {
    system.shutdown()
    super.after
  }
}
Run Code Online (Sandbox Code Playgroud)

scala akka specs2

6
推荐指数
1
解决办法
2218
查看次数

如何匹配抛出的Exception消息?

specs2 Matchers指南指出:

throwA[ExceptionType](message = "boom") 另外检查异常消息是否符合预期

但是当我使用它时,消息显然在整个堆栈跟踪上匹配,而不仅仅是异常消息.

测试

"cont'd what if -- running test again shows that the app has already died" in {
  running(new FakeApplication(additionalConfiguration = inLocalPostgresDatabase())) {
    db2 withSession {
      val comboboxOpsClass = new ComboboxOps(database)
    }
  } must throwA[SQLException](message = "Attempting to obtain a connection from a pool that has already been shutdown")
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪

[error]      'Attempting to obtain a connection from a pool that has already been shutdown. 
[error]      Stack trace of location where pool was …
Run Code Online (Sandbox Code Playgroud)

scala specs2

6
推荐指数
1
解决办法
3782
查看次数

如何使用playswork 2.3 with specs2 2.4而不是specs2 2.3.x.

最近,specs2更新到版本2.4,现在使用scalaz 7.1而不是7.0.x. 一旦我在我的游戏中更新我的specs2依赖项!2.3项目使用版本2.4,所有测试都失败,但有以下异常:

 [error] Uncaught exception when running ...Spec: java.lang.In
 compatibleClassChangeError: Found class scalaz.syntax.FunctorOps, but interface
 was expected
 sbt.ForkMain$ForkError: Found class scalaz.syntax.FunctorOps, but interface was
 expected
    at org.specs2.specification.SpecificationStructure$.createSpecificationEither(BaseSpecification.scala:119)
    at org.specs2.runner.SbtRunner.org$specs2$runner$SbtRunner$$specificationRun(SbtRunner.scala:73)
    at org.specs2.runner.SbtRunner$$anonfun$newTask$1$$anon$5.execute(SbtRunner.scala:59)
    at sbt.ForkMain$Run$2.call(ForkMain.java:294)
    at sbt.ForkMain$Run$2.call(ForkMain.java:284)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Run Code Online (Sandbox Code Playgroud)

之前似乎没有人有这个错误.至少我无法在specs2和play项目的问题跟踪系统中找到它.

specs2 scalaz7 playframework-2.3

6
推荐指数
2
解决办法
1533
查看次数

如何设置SBT构建以在Jenkins的测试失败时返回零退出代码?

当我通过SBT在Jenkins中运行我的Specs2测试时,一旦测试失败,构建就会被标记为失败.由于Jenkins通常区分构建失败和测试失败,我想改变这一点.

我知道Jenkins的构建失败是由对SBT的调用的退出代码检测到的,一旦至少一个测试失败,它就会返回1.

有什么选择我假设我想避免改变我build.sbt(或一般的项目)只是为了解决这个不便?

不知何故,我认为应该可以将标准的sbt项目放入标准的Jenkins安装中并使其按预期工作.

sbt scalatest jenkins specs2

6
推荐指数
1
解决办法
1512
查看次数

Stacktraces用于Specs2输出中的用户实例化异常

在我们的测试中,我们广泛使用存根,其中一些创建并抛出异常.我发现只是实例化一个异常,导致堆栈跟踪在我们的测试运行时出现.这意味着存在大量不必要和混乱的噪音,因为我们不关心这些"预期的"例外情况.我已经浏览了互联网,但找不到任何关于沉默这些堆栈跟踪的内容(记住,实际异常,在代码中的其他地方抛出,或者通过框架,我仍然希望显示跟踪).这是一个常见问题,有没有人找到解决办法?

对不起,举个例子:

val t = new Throwable("Expected exception")
val service = new AuthenticationService()(ExceptionThrowingClient(t))
Run Code Online (Sandbox Code Playgroud)

使用类似于下面的堆栈跟踪进行测试运行的结果:

java.lang.Throwable: Expected exception
at services.auth.AuthenticationServiceSpec$$anonfun$4.apply(ServiceSpec.scala:104) ~[test-classes/:na]
at services.auth.AuthenticationServiceSpec$$anonfun$4.apply(ServiceSpec.scala:103) ~[test-classes/:na]
at org.specs2.mutable.SideEffectingCreationPaths$$anonfun$executeBlock$1.apply$mcV$sp(FragmentsBuilder.scala:292) ~[specs2-core_2.10-2.3.12.jar:2.3.12]
at org.specs2.mutable.SideEffectingCreationPaths$class.replay(FragmentsBuilder.scala:264) ~[specs2-core_2.10-2.3.12.jar:2.3.12]
at org.specs2.mutable.Specification.replay(Specification.scala:12) ~[specs2-core_2.10-2.3.12.jar:2.3.12]
Run Code Online (Sandbox Code Playgroud)

其中追溯到规范中的实例化点.

error-handling scala playframework specs2 playframework-2.3

6
推荐指数
0
解决办法
103
查看次数

在Play Framework 2.4.2中使用scalates/specs2测试HttpErrorHandler

我在Play Framework 2.4.2中实现了自己的HttpErrorHander,它运行得很好,但现在我希望能够使用故意抛出异常的"Fake Actions"进行测试.我已经在尝试scalatestspecs2

import play.api.http.HttpErrorHandler
import play.api.mvc._
import play.api.mvc.Results._
import scala.concurrent._

class MyErrorHandler extends HttpErrorHandler {

  def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
    Future.successful(
      Status(statusCode)("A client error occurred: " + message)
   )
  }

  def onServerError(request: RequestHeader, exception: Throwable) = {
    Future.successful(
      InternalServerError("A server error occurred: " + exception.getMessage)
    )
  }
}
Run Code Online (Sandbox Code Playgroud)

我到目前为止尝试了以下测试.我尝试调试代码,但我从不输入我的方法.方法play.api.http.DefaultHttpErrorHandler既没有执行.

object ThrowableControllerSpec extends PlaySpecification with Results {

  "Example Page" should {


    "throwErrorAction should be valid" in {
       val controller = …
Run Code Online (Sandbox Code Playgroud)

scala scalatest playframework specs2 playframework-2.4

6
推荐指数
1
解决办法
885
查看次数