scalaziozio-test

Assert a failed effect with ZIO2 and zio-test


I am a beginner in the Scala / ZIO 2 world, and I am trying to write some tests for a simple service.

so I have this method:

def validate(id: String): ZIO[Any, Throwable, Unit] = {
  if (id == "invalid-id") {
    ZIO.fail("Invalid id")
  }
}

I tried several things, but mainly I tried to use the isFailure or the fails assertions:

 suite("My suite")(
    test("When id is valid") { // This passes
      for {
        result <- validate("valid-id")
      } yield assertTrue(result == ())
    },
    test("when id is not valid") { 
      for {
        result <- validate("invalid-id")
      } yield assertTrue(isFailure(result)) // This doesn't even compile
    }
  )

How can I test the failure case of an effect?

I am using:

Scala: "3.2.1"
zio: "2.0.4"
zio-test: "2.0.5"

Solution

  • There are multiple ways to assert that an effect has failed. The below example demonstrates the use of ZIO#exit and ZIO#flip.

    import zio._
    import zio.test._
    
    object MySpec extends ZIOSpecDefault {
    
      def validate(id: String): ZIO[Any, String, Unit] =
        ZIO.cond(id != "invalid-id", (), "Invalid id")
    
      def spec =
        suite("My suite")(
    
          test("when id is not valid 1") {
            for {
              result <- validate("invalid-id").exit
            } yield assertTrue(
              result == Exit.fail("Invalid id")
            )
          },
    
          test("when id is not valid 2") {
            for {
              result <- validate("invalid-id").flip
            } yield assertTrue(
              result == "Invalid id"
            )
          }
        )
    }