我正在使用该BlazeClientBuilder[IO].resource方法来获取Client[IO]. 现在,我想模拟客户端进行单元测试,但不知道该怎么做。有没有一个好的方法来嘲笑这个,我会怎么做?
class ExternalCall(val resource: Resource[IO, Client[IO]], externalServiceUrl: Uri) {
def retrieveData: IO[Either[Throwable, String]] = {
for {
req <- IO(Request[IO](Method.GET, uri = externalServiceUrl))
response <- resource.use(client => {
client.fetch[String](req)(httpResponse => {
if (!httpResponse.status.isSuccess)
throw new Exception(httpResponse.status.reason)
else
httpResponse.as[String]
})
})
} yield Right(response)
}
}
Run Code Online (Sandbox Code Playgroud)
来电代码
new ExternalCall(BlazeClientBuilder[IO](global).resource).retrieveData
Run Code Online (Sandbox Code Playgroud) 我正在阅读-nophytes-guide-to-scala-part-10,其中我遇到了以下代码.
type EmailFilter = Email => Boolean
val minimumSize: Int => EmailFilter = n => email => email.text.size >= n
Run Code Online (Sandbox Code Playgroud)
我理解了第一行,其中为一个函数创建了类型别名EmailFilter,该函数接收电子邮件返回布尔值.但我不明白我们将电子邮件和数字作为输入的第二行,并通过检查大小返回布尔值.请解码第二行并向我解释该函数的语法糖代码.
我有一个像这样的字符串到IO的映射Map[String, IO[String]],我想将其转换为IO[Map[String, String]].怎么做?
考虑一个案例类Person
case class Person(firstName: String, lastName: String, middleName: Option[String])
object Person {
def apply(firstName: String, lastName: String): Person = new Person(firstName, lastName, None)
def unapply(arg: Person): Option[(String, String)] = Some(arg.firstName, arg.lastName)
}
val person = Person("firstName", "lastName")
person match {
case Person(firstName, lastName) => Console.println(firstName + " " + lastName)
}
Run Code Online (Sandbox Code Playgroud)
在与case类编译器的模式匹配中给出了一个错误:模式匹配中的参数数量错误,但是当我使用类而不是case类时它的工作.
class Person(val firstName: String, val lastName: String, middleName: Option[String])
Run Code Online (Sandbox Code Playgroud)
我在这里理解的是,我们不能为case类使用自己的自定义提取器,但可以使用自己的构造函数(apply).请解释我这种奇怪的行为.
这是一个测试的ScalaFiddle:https://scalafiddle.io/sf/HvxvdAZ/0