...

/

Unit Tests for Product Routes Part 1

Unit Tests for Product Routes Part 1

Learn how to test the endpoints for querying existing and non-existing products.

Querying a non-existing product

Press + to interact
val emptyRepository: Repository[IO] = new TestRepository[IO](Seq.empty)
val expectedStatusCode = Status.NotFound
s"return$expectedStatusCode"in{
forAll("id") { id: ProductId =>
Uri.fromString("/product/" + id.toString) match {
case Left(_) => fail("Could not generate valid URI!")
case Right(u) =>
def service: HttpRoutes[IO] =
Router("/" -> new ProductRoutes(emptyRepository).routes)
val response: IO[Response[IO]] = service.orNotFound.run(
Request(method = Method.GET, uri = u)
)
val result = response.unsafeRunSync
result.status must be(expectedStatusCode)
result.body.compile.toVector.unsafeRunSync must be(empty)
}
}
}

Above, you can see the test for querying a non-existing product. This must return an empty response using a 404 Not Found status code (Line 2).

First, we try to create a valid URI from our generated ...

Access this course and 1400+ top-rated courses and projects.