0

必須パラメーター adId を必要とする scalatest を使用して POST スプレー ルートをテストしようとしています。そしてそれを機能させることはできません。私のコードは次のとおりです

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.{StatusCodes, MediaTypes}
import spray.testkit.ScalatestRouteTest

class AdServiceApiTest extends  WordSpecLike with MustMatchers with ScalatestRouteTest{
 "AdService REST api " must{
   "POST for import witout mandatory parameters should fail with " in{
      val p = TestProbe()
      val addressServiceMock = system.actorOf(Props(classOf[AdServiceActorMock],p.ref))

      Post("/service/ad/import") ~> new AdServiceApi(addressServiceMock).route ~>check{
        handled must be(false)
        status must be (StatusCodes.BadRequest)
      }
    }
  }

テストは失敗しますが、別の理由で

Request was rejected with List(MissingQueryParamRejection(adId))
org.scalatest.exceptions.TestFailedException: Request was rejected with List(MissingQueryParamRejection(adId))
    at spray.testkit.ScalatestInterface$class.failTest(ScalatestInterface.scala:25)
    at com.ss.rg.api.ad.AdServiceApiTest.failTest(AdServiceApiTest.scala:19)
    at spray.testkit.RouteResultComponent$RouteResult$$anonfun$response$1$$anonfun$apply$1.apply(RouteResultComponent.scala:97)
    at spray.testkit.RouteResultComponent$RouteResult$$anonfun$response$1$$anonfun$apply$1.apply(RouteResultComponent.scala:95)
    at scala.Option.foreach(Option.scala:236)
    at spray.testkit.RouteResultComponent$RouteResult$$anonfun$response$1.apply(RouteResultComponent.scala:94)
...

ステータスすらチェックされていないようです。私には完全にクリアされていない2番目のことは、実際にspray-testkitでadIdパラメータを設定する方法ですか? 1 つの方法は、ヘッダーを設定することですが、より良い方法が存在することに驚かないでしょう。

スプレーテストキットの経験が豊富な人がコメントできますか?

どうも

4

1 に答える 1

1

ステータスはありません - ルートがリクエストを拒否しました。拒否にアクセスしてrejection、それが期待するタイプであることをアサートできます。ブラウザーが実際に何を表示するかを確認したい場合はhandleRejections、デフォルトを使用してディレクティブでルートをラップする必要がありますRejectionHandler(暗黙的に利用可能です)。そうすれば、期待するステータス コードを表示できます。その場合、(ラップされたルートがリクエストを処理するため、失敗したステータスコードとエラーメッセージを含むレスポンスを返すため)handled出てきます。true

于 2015-03-16T15:52:51.923 に答える