首页 > 解决方案 > 有没有办法模拟未作为服务参数接收的 WSClient - Scala + PlayFramework

问题描述

我有这项服务:

    def doNext(token: String): Future[Location] = {
        val config = getWsConfig(prefix)
        ws.url(config.url)
          .map(response => (response.json \ "location").get.asInstanceOf[JsString].value)
      }

我正在尝试对其进行测试,但我无法模拟 WSClient,因为它不是该服务接收的参数。我怎么能测试这个?

这是我的测试 ATM

  "Spec" should {
    "giberish" in {
      Server.withRouterFromComponents() { components =>
        import Results._
        import components.{ defaultActionBuilder => Action }

        {
          case GET(p"/do-next") =>
            Action {
              Ok(Json.arr(Json.obj("location" -> "USA")))
            }
        }
      } { implicit port =>
        WsTestClient.withClient { client =>
          val user = buildUser()
          val result = Await.result(instanceOf[ActionService].doAction("example-user_token")(user), 10.seconds)
          result shouldBe "USA"
        }
      }
    }
  }
java.net.ConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:8080

标签: scalaplayframeworkmockito

解决方案


推荐阅读