...

/

Testing the Routes: Use Case - Updating

Testing the Routes: Use Case - Updating

Learn how to test the endpoints for updating a non-existing product, an existing product and updating with a garbage value.

Garbage values

Press + to interact
val expectedStatus = StatusCodes.BadRequest
s"return $expectedStatus" in {
genProduct.sample match {
case None => fail("Could not generate data sample!")
case Some(p) =>
for {
_ <- repo.saveProduct(p)
rows <- repo.loadProduct(p.id)
resp <- http.singleRequest(
HttpRequest(
method = HttpMethods.PUT,
uri = s"$baseUrl/product/${p.id}",
headers = Seq(),
entity = HttpEntity(
contentType = ContentTypes.`application/json`,
data = ByteString(scala.util.Random.alphanumeric.take(256).mkString)
)
)
)
rows2 <- repo.loadProduct(p.id)
} yield {
withClue("Seeding product data failed!")(rows must not be(empty))
resp.status must be(expectedStatus)
Product.fromDatabase(rows2) match {
case None =>
fail("Seeding product was not saved to database!")
case Some(s) =>
withClue("Existing product must not be changed!")(s mustEqual p)
}
}
}
}

First, we test with garbage JSON in the request. Before making the request, we create a product to avoid getting an error caused by a possibility of ...

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