I am having a hard time trying to unit test F# code with external dependencies.
In C# (my background) you would typically have a class with a dependency passed in, which is then re-used. Apologies for my sample code, it's dumb but I'm just trying to illustrate my point.
public class Foo {
IDependency d;
public Foo(IDependency d) { this.d = d; }
public int DoStuff(string bar) { return d.DoSomethingToStuff(bar); }
public int DoMoreStuff(string bar) {
int i = d.DoSomethingToStuff(bar);
return d.DoSomethingElseToStuff(bar, i);
}
}
I'm trying to be pragmatic with F# and avoid using classes and interfaces (unless I need to interop with other .NET languages).
So my approach in this scenario is to have module and some functions with the dependencies passed in as functions. I found this tecnique here
module Foo
let doStuff bar somethingFunc =
somethingFunc bar
let doMoreStuff bar somethingFunc somethingElseFunc =
let i = somethingFunc bar
somethingElseFunc bar i
The two problems I have with this code is:
I need to keep passing my dependencies around. In C#, it's passed in the constructor and re-used. You can imagine how quickly this gets out of control if
somethingFunc
is used in several places.How do I unit test that dependencies have been executed? Again in C# I'd use a mocking framework and assert that certain methods were called.
How do I approach these problems in the F# world?