Scala 使用SttpBackendStub的单元测试流请求

Scala 使用SttpBackendStub的单元测试流请求,scala,akka-stream,sttp,Scala,Akka Stream,Sttp,我有一个请求,它返回我想要测试的源流,如下所示: /*阶级*/ 导入sttp.client_ 类HttpClient 隐式sttpBackend:sttpBackend[Future,Source[ByteString,Any],Nothing]{ /**启动大型文件的下载流*/ def downloadStreamurl:String:Future[Response[String,Source[ByteString,q]]]={ 要求 .geturiurl .responseastream[S

我有一个请求,它返回我想要测试的源流,如下所示:

/*阶级*/ 导入sttp.client_ 类HttpClient 隐式sttpBackend:sttpBackend[Future,Source[ByteString,Any],Nothing]{ /**启动大型文件的下载流*/ def downloadStreamurl:String:Future[Response[String,Source[ByteString,q]]]={ 要求 .geturiurl .responseastream[Source[ByteString,Any]] 邮寄 } } /*试验*/ 导入org.scalatest.FlatSpec 导入org.scalatest.Matchers 类HttpClientSpec使用匹配器扩展了FlatSpec{ 隐式val as=ActorSystem 隐式val mat=Actormatarializer 隐式val ex=as downloadStream应该从中的url获取文件流{ val url=shttp://example.com/api/v1/file/Test.txt val body=abcdefghijklmnopqrstuzwxyz val expectedStream=Source.single[ByteString]body.getBytes 隐式val stubBackend=SttpBackendStub.asynchronousFuture .whenRequestMatchesreq=>req.method.method==GET&&req.uri==uri$url .然后回答 ResponseRightexpectedStream,状态代码。确定 val httpTest=新的HttpClient val response=Await.resulthtptest.downloadStreamurl,5.5秒 response.body.right.get应为ExpectedStream } } 但是它失败了,因为SttpBackendStub.asynchronousFuture

类型失配; 找到[错误]:sttp.client.testing.SttpBackendStub[scala.concurrent.Future,Nothing] [错误]必需:sttp.client.SttpBackend[scala.concurrent.Future,akka.stream.scaladsl.Source[akka.util.ByteString,Any],无任何内容]
如何使用SttpBackendStub而不使用scalamock之类的工具?

只需创建自己的Stub类来扩展预期的响应类型:

类StreamHttpStub 扩展SttpBackendStub[Future,Source[ByteString,Any]]new FutureMonad,PartialFunction.empty,None{ } 隐式val stubBackend=新的StreamHttpStub .whenRequestMatchesreq=>req.method.method==GET&&req.uri==uri$url .然后回答 ResponseRightexpectedStream,状态代码。确定