Here's a sample unit test that tests if the response is the one expected.
open Alcotest
open Lwt.Syntax
open Rock
open Opium_testing
let test_case n f = Alcotest_lwt.test_case n `Quick (fun _switch () -> f ())
let handle_request = handle_request My_app.app
let suite =
[ ( "POST /create-user"
, [ test_case "redirects to show when data is valid" (fun () ->
let req =
Request.of_json
~body:(`Assoc [ "name", `String "Tom" ])
"/create-user"
`POST
in
let+ res = handle_request req in
check_status `Found res.status)
; test_case "renders errors when data is invalid" (fun () ->
let req =
Request.of_json
~body:(`Assoc [ "invalid-key", `String "invald-value" ])
"/create-user"
`POST
in
let+ res = handle_request req in
check_status `Bad_request res.status;
check_body_contains "Invalid data" res.body)
] )
]
;;
let () = Lwt_main.run @@ Alcotest_lwt.run "User Handler" suite
API documentation
Opium_testingThis module provides helpers to easily test Opium applications with Alcotest.