【问题标题】:spray-testkit === status passes whatever status isspray-testkit === status 通过任何状态
【发布时间】:2015-03-16 16:35:05
【问题描述】:

我在 Spray-testkit 中使用 scalatest 进行了以下测试,但我不明白为什么尽管实际状态如此,但每次都通过测试。

import akka.actor._
import akka.event.LoggingReceive
import akka.testkit.TestProbe
import com.ss.rg.service.ad.AdImporterServiceActor.{GetImportStatus, StatusOfImport}
import org.scalatest.{MustMatchers, WordSpecLike}
import spray.http._
import spray.testkit.ScalatestRouteTest


class AdServiceApiTest extends WordSpecLike with MustMatchers with ScalatestRouteTest {

  "AdService REST api " must {
    "POST for import with mandatory parameters should be processed" in {
      val p = TestProbe()
      val addressServiceMock = system.actorOf(Props(classOf[AdServiceActorMock], p.ref))

      Post("/service/ad/import?adId=1") ~> new AdServiceApi(addressServiceMock).route ~> check {
        handled must be(true)

//        rejections must have size 1
        response.status === StatusCodes.NotAcceptable
      }
    }
  }

测试通过,即使状态应该不同,当我因为明显的原因(如拒绝)测试失败时......错误消息如下:

Request was not rejected, response was HttpResponse(202 Accepted,HttpEntity(text/plain; charset=UTF-8,The request has been accepted for processing, but the processing has not been completed.),List(),HTTP/1.1)
org.scalatest.exceptions.TestFailedException: Request was not rejected, response was HttpResponse(202 Accepted,HttpEntity(text/plain; charset=UTF-8,The request has been accepted for processing, but the processing has not been completed.),List(),HTTP/1.1)
    at spray.testkit.ScalatestInterface$class.failTest(ScalatestInterface.scala:25)

似乎我在这里遗漏了一些重要的概念。

有人可以澄清一下吗?

更新:以下工作正常

status must equal(StatusCodes.Accepted)

【问题讨论】:

    标签: scala akka spray spray-dsl spray-test


    【解决方案1】:

    === 还不够。你必须写

    status should === (StatusCodes.NotAcceptable)
    

    assert(status === StatusCodes.NotAcceptable)
    

    【讨论】:

    • 添加一点背景,=== 只是一个类型安全的 equals 方法,因此它不会抛出异常(也不应该)。
    • 使用-Ywarn-value-discard 编译可以帮助解决此类问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多