I'm new in cats and functional programming and I'm struggling with unit testing functional data types like EitherT. Having example code:
class Library[F[_]]() {
def create(book: Book)(implicit M: Monad[F]): EitherT[F, BookAlreadyExistsError, Book] = ...
}
I'd like to test it using Spec2 but I don't know how to do it properly. Tried something like this but it does not work:
val library = Library[IO]()
test("create book") {
val book = Book("Title 1", 2016, "author 1")
(for (
resultBook <- library.create(book)
) yield resultBook shouldEqual ???
).unsafeRunSync()
}
I'd like to have very simple assertions like this:
resultBook shouldEqual Right(Book("Title 1", 2016, "author 1"))
// or
resultBook shouldEqual Left(BookAlreadyExistsError)
specs2-catsprovidesIOMatcherstrait which enables the following syntaxwhere
Here is a working example