[英]ScalaTest AsyncFunSuiteLike multiple asserts
I am rewriting tests that used to test an Akka actor that responded with a String, right now, we use a function that returns a Future[String]
.我正在重写用于测试以 String 响应的 Akka actor 的测试,现在,我们使用一个返回
Future[String]
的函数。 So rewriting the tests using AsyncFunSuiteLike.所以使用 AsyncFunSuiteLike 重写测试。
Currently I'm looking at a test that has to assert multiple Futures, but I don't seem to understand how to achieve it with AsyncFunSuiteLike.目前我正在研究一个必须断言多个 Futures 的测试,但我似乎不明白如何使用 AsyncFunSuiteLike 来实现它。
My test looks as follows:我的测试如下:
test("Error responses") {
NanoHTTPD.Response.Status.values().filter(status => status.getRequestStatus >= 400).map {
status => {
val statusCode = status.getRequestStatus
httpService.setStatusCode(status)
val responseBody = s"Request failed with status $status"
httpService.setResponseContent(responseBody)
val errorMessage = s"Error response (${status.getRequestStatus}): $responseBody"
myobject.request("123456").map {
resp => assert(resp === "....")
}
}
}
}
But I am getting an error type missmatch. Expected: Future[Assertion] actual: List[Future[scalatest.Assertion]]
但我收到错误
type missmatch. Expected: Future[Assertion] actual: List[Future[scalatest.Assertion]]
type missmatch. Expected: Future[Assertion] actual: List[Future[scalatest.Assertion]]
Any idea on how can I achieve multiple assertions within a single test like that?关于如何在这样的单个测试中实现多个断言的任何想法?
ScalaTest asynchronous testing expects the test body to return Future[Assertion]
. ScalaTest异步测试期望测试主体返回
Future[Assertion]
。 Now your test body returns List[Future[Assertion]]]
because现在你的测试体返回
List[Future[Assertion]]]
因为
NanoHTTPD.Response.Status.values() ... // Returns List
...
myobject.request("123456").map(resp => assert(resp === "....")} // Returns Future[Assertion]
...
}
We can convert List[Future[T]]
to Future[List[T]]
using我们可以将
List[Future[T]]
转换为Future[List[T]]
使用
Future.sequence(listOfFutures)
and, we can convert List[Assertion]
to Assertion
using并且,我们可以使用以下方法将
List[Assertion]
转换为Assertion
assert(listOfAssertions.forall(_ == Succeeded))
Putting this together we get把这个放在一起我们得到
Future.sequence {
NanoHTTPD.Response.Status.values().filter(status => status.getRequestStatus >= 400).map {
status => {
val statusCode = status.getRequestStatus
httpService.setStatusCode(status)
val responseBody = s"Request failed with status $status"
httpService.setResponseContent(responseBody)
val errorMessage = s"Error response (${status.getRequestStatus}): $responseBody"
myobject.request("123456").map {
resp => assert(resp === "....")
}
}
}
}.map(listOfAssertions => assert(listOfAssertions.forall(_ == Succeeded)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.