1

我有一个简单的“保存”功能正在使用akka-stream-alpakka multipartUpload,它看起来像这样:

  def save(fileName: String): Future[AWSLocation] = {

    val uuid: String = s"${UUID.randomUUID()}"

    val s3Sink: Sink[ByteString, Future[MultipartUploadResult]] = s3Client.multipartUpload(s"$bucketName", s"$uuid/$fileName")

    val file = Paths.get(s"/tmp/$fileName")

    FileIO.fromPath(file).runWith(s3Sink).map(res => {
      AWSLocation(uuid, fileName, res.key)
    }).recover {
      case ex: S3Exception =>
        logger.error("Upload to S3 failed with s3 exception", ex)
        throw ex
      case ex: Throwable =>
        logger.error("Upload to S3 failed with an unknown exception", ex)
        throw ex
    }
  }

我想测试这个功能,2种情况:

  1. 那 multipartUpload 成功了,我得到了 AWSLocation (我的案例类)。
  2. multipartUpload 失败,我得到 S3Exception

所以我想监视 multipartUpload 并返回我自己的接收器,如下所示:

  val mockAmazonS3ProxyService: S3ClientProxy = mock[S3ClientProxy]

  val s3serviceMock: S3Service = mock[S3Service]

  override val fakeApplication: Application = GuiceApplicationBuilder()
    .overrides(bind[S3ClientProxy].toInstance(mockAmazonS3ProxyService))
    .router(Router.empty).build()

  "test" in {
    when(mockAmazonS3ProxyService.multipartUpload(anyString(), anyString())) thenReturn Sink(ByteString.empty, Future.successful(MultipartUploadResult(Uri(""),"","myKey123","",Some(""))))

    val res = s3serviceMock.save("someFileName").futureValue

    res.key shouldBe "myKey123"

  }

问题是我明白了Error:(47, 93) akka.stream.scaladsl.Sink.type does not take parameters,我知道我不能像这样创建水槽,但我该怎么做?或者有什么更好的测试方法?

4

1 回答 1

1

考虑重新设计您的方法save,使其更具可测试性,并且可以注入针对不同测试产生不同结果的特定接收器(如 Bennie Krijger 所述)。

  def save(fileName: String): Future[AWSLocation] = {
    val uuid: String = s"${UUID.randomUUID()}"
    save(fileName)(() => s3Client.multipartUpload(s"$bucketName", s"$uuid/$fileName"))
  }

  def save(
    fileName: String
  )(createS3UploadSink: () => Sink[ByteString, Future[MultipartUploadResult]]): Future[AWSLocation] = {

    val s3Sink: Sink[ByteString, Future[MultipartUploadResult]] = createS3UploadSink()

    val file = Paths.get(s"/tmp/$fileName")

    FileIO
      .fromPath(file)
      .runWith(s3Sink)
      .map(res => {
        AWSLocation(uuid, fileName, res.key)
      })
      .recover {
        case ex: S3Exception =>
          logger.error("Upload to S3 failed with s3 exception", ex)
          throw ex
        case ex: Throwable =>
          logger.error("Upload to S3 failed with an unknown exception", ex)
          throw ex
      }
  }

测试看起来像

class MultipartUploadSpec extends TestKit(ActorSystem("multipartUpload")) with FunSpecLike {

  implicit val mat: Materializer = ActorMaterializer()

  describe("multipartUpload") {
    it("should pass failure") {
      val result = save(() => Sink.ignore.mapMaterializedValue(_ => Future.failed(new RuntimeException)))
      // assert result
    }

    it("should pass successfully") {
      val result = save(() => Sink.ignore.mapMaterializedValue(_ => Future.successful(new MultipartUploadResult(???))))
      // assert result
    }
  }
于 2018-12-05T11:39:21.370 回答