我想在不调用 fun2 的情况下对 fun1 进行单元测试。
let fun2() =
// Some complex function with lots of dependencies.
1
let fun1() =
fun2() * 2
打破两个函数之间依赖关系的最佳方法是什么?
我尝试了几种不同的方法,但它们只会增加混乱。
将 fun2 传递给 fun1
let fun1(fun2) =
fun2() * 2
转换为类并覆盖
type FunClass() =
abstract member fun2 : unit -> int
default x.fun2() = 1
member x.fun1() =
x.fun2() * 2
type FunClassMock() =
override member x.fun2() = 1
使用策略模式
type Fun1Class(fun2Class) =
member x.fun1() =
fun2Class.fun2() * 2
使用变量
let fun2Imp() =
1
let mutable fun2 = fun2Imp
let fun1() =
fun2() * 2
有没有更清洁的方法?