import zio._
import zio.test._
import zio.test.mock._
trait ExternalService {
def fetchData(): UIO[String]
}
def businessLogic(): ZIO[ExternalService, Throwable, String] =
ZIO.accessM(_.fetchData())
object BusinessLogicSpec extends DefaultRunnableSpec {
val mockEnv: ULayer[Has[ExternalService]] =
ZLayer.succeed(MockExternalService.Mock)
val testLayer: URLayer[Has[ExternalService], Has[ExternalService]] =
ZLayer.fromService[ExternalService, ExternalService](identity)
def spec: ZSpec[Environment, Failure] =
suite("BusinessLogicSpec")(
testM("businessLogic should return the expected result") {
for {
result <- businessLogic().provideSomeLayer(testLayer)
} yield assert(result)(equalTo("Mocked Data"))
}
).provideCustomLayer(mockEnv)
}