Opium Testing
opium-testing
provides helpers to easily test Opium applications with Alcotest.
Installation
To install opium-testing
, you can add it to your dune-project
's package stanza:
(package (name my_app) (depends (ocaml (>= 4.08)) (dune (>= 2.0)) rock (alcotest :with-test) (alcotest-lwt :with-test) (opium-testing :with-test)))
And let Dune generate the Opam file for you. Or you can add it directly to your .opam
file:
depends: [ "alcotest" {with-test} "alcotest-lwt" {with-test} "opium-testing" {with-test} ]
Usage
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