ファンクタを使用してモジュールをビルドします。ファンクタを引数としてConnectionモジュールを使用します。次に、テストでConnectionモジュールをスタブアウトすることができます。
だから、例えば、あなたのdb.mlファイルは一種の次のようになります。
(* 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
は、その後、あなたのtest_db.mlであなただけの接続モジュールに
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"])
をスタブアウトすることができます