使用泛型的 ZIO Mock 方法
ZIO Mock method that works with generics
我正在尝试模拟具有通用方法的服务,但我不知道如何在模拟中定义效果。通用效果甚至可以工作吗?有什么解决方法吗?
服务:
object AService {
type AService = Has[Service]
trait Service {
def add(n1: Int, n2: Int): Task[Int]
def foo[A](n1: A, n2: A): Task[A]
}
def live: ULayer[Has[Service]] = ...
}
模拟:
object AServiceMock extends Mock[AService] {
object Add extends Effect[(Int, Int), Nothing, Int]
object Foo[T] extends Effect[(T, T), Nothing, T] // <- Can I make something like this work?
override protected[test] val compose = ZLayer.fromService {proxy =>
new AService.Service {
override def add(n1: Int, n2: Int): Task[Int] = proxy(Add, n1, n2)
override def foo[T](n1: T, n2: T): Task[T] = proxy(Foo, n1, n2)
}
}
}
PS:我也尝试过 @mockable[AService.Service]
,但我收到:“宏扩展期间出现异常:需要标识符,但 $XMLSTART$< 找到了”
谢谢
已解决。答案在 ZIO documentation
def foo[A: zio.Tag](n1: A, n2: A): Task[A]
----------------
object AServiceMock extends Mock[AService] {
object Add extends Effect[(Int, Int), Nothing, Int]
object Foo extends Poly.Effect.InputOutput[Nothing]
val compose: URLayer[Has[mock.Proxy], AService] = ZLayer.fromService { proxy =>
new AService.Service {
override def add(n1: Int, n2: Int): Task[Int] = proxy(Add, n1, n2)
override def foo[A: Tag](n1: A, n2: A): Task[A] = proxy(Foo.of[(A, A), A], n1, n2)
}
}
}
我正在尝试模拟具有通用方法的服务,但我不知道如何在模拟中定义效果。通用效果甚至可以工作吗?有什么解决方法吗?
服务:
object AService {
type AService = Has[Service]
trait Service {
def add(n1: Int, n2: Int): Task[Int]
def foo[A](n1: A, n2: A): Task[A]
}
def live: ULayer[Has[Service]] = ...
}
模拟:
object AServiceMock extends Mock[AService] {
object Add extends Effect[(Int, Int), Nothing, Int]
object Foo[T] extends Effect[(T, T), Nothing, T] // <- Can I make something like this work?
override protected[test] val compose = ZLayer.fromService {proxy =>
new AService.Service {
override def add(n1: Int, n2: Int): Task[Int] = proxy(Add, n1, n2)
override def foo[T](n1: T, n2: T): Task[T] = proxy(Foo, n1, n2)
}
}
}
PS:我也尝试过 @mockable[AService.Service]
,但我收到:“宏扩展期间出现异常:需要标识符,但 $XMLSTART$< 找到了”
谢谢
已解决。答案在 ZIO documentation
def foo[A: zio.Tag](n1: A, n2: A): Task[A]
----------------
object AServiceMock extends Mock[AService] {
object Add extends Effect[(Int, Int), Nothing, Int]
object Foo extends Poly.Effect.InputOutput[Nothing]
val compose: URLayer[Has[mock.Proxy], AService] = ZLayer.fromService { proxy =>
new AService.Service {
override def add(n1: Int, n2: Int): Task[Int] = proxy(Add, n1, n2)
override def foo[A: Tag](n1: A, n2: A): Task[A] = proxy(Foo.of[(A, A), A], n1, n2)
}
}
}