ziozio-test

scala console error when testing sample IO using zio


Hi I've below ZIO ConsoleIO test.

import zio._
import zio.Console
import zio.test.Assertion.equalTo
import zio.test._

object ConsoleIOSpec extends ZIOSpecDefault {
  val greet: ZIO[Any, Nothing, Unit] = for {
    name <- Console.readLine.orDie
    _ <- Console.printLine(s"Hello, $name").orDie
  } yield ()

  def spec = suite("ExampleSpec")(
    test("greet says hello to the user") {
      for {
        _ <- TestConsole.feedLines("Jack")
        _ <- greet
        value <- TestConsole.output
      } yield assert(value) (equalTo(Vector("Hello, Jack")))
    }
  )
}

The code is on this github here. When I run the test I get the below error -

  • ExampleSpec / greet says hello to the user āœ— Vector1("Hello, Jack ") was not equal to Vector1("Hello, Jack") value did not satisfy equalTo(Vector("Hello, Jack")) value = Vector1("Hello, Jack ") at /Users/rnatarajan/Documents/Coding/others/zionomicon/c2-testing/src/test/scala/ConsoleIOSpec.scala:18

           Output Produced by Test         
    

    | Jack | Hello, Jack

I've attached the error screenshot -

enter image description here

This is code from zionomicon book section 3.3 Test Implementations Of Standard ZIO Services.

How to fix this issue?


Solution

  • Method "Console.printLine" prints the string in parameter, and a carriage return at the end of it, as you can see with the double quotes in a new line. To fix the problem, you can replace "Console.printLine" with "Console.print", or take into account the return carriage at the end of the output.