Create your module using a functor that takes a Connection module as an argument. Then you can stub the Connection module in your tests.
So, for example, your db.ml file might look something like this:
(* The interface of Connection that we use *) module type CONNECTION = sig type t val execute : string -> t -> string list end (* functor to build Db modules, given a Connection module *) module Make(Connection : CONNECTION) = struct ... let get_data connection = do_something_with (Connection.execute "some query" connection) ... end
Then in your test_db.ml you can just close the Connection module
let test_get_data () = let module TestConnection = struct type t = unit let execute _ _ = ["data"] end in let module TestDb = Db.Make(TestConnection) in assert (TestDb.get_data () = ["munged data"])
rapha source share