I'm trying to write a test for a small lib I'm writing that (essentially) just logs to the console.
Is there a way to mock function like this is F#?
example: in src/Lib/Lib.fs
module Lib
let print msg = printfn "Your message is: %s" msg
then, in test/LibTest/Lib.fs
module LibTest
open NUnit.Framework
open FsUnit
[<Test>]
let ``should print what I expect``() =
print "test" |> should equal "Your message is: test"
Note: I'm aware that currently print returns unit - I am looking for a way to make assertions about what is passed to printfn (or, more ideally, what is sent to stdout, which relies less on implementation).
I've tried directly assigning a mock function to Printf.printfn to no avail (obviously, when I think about it). Is it possible to capture the output to the console? Or to mock the printfn function (it's an implementation detail, but I can live with that).
This is what I use for testing:
The code I am testing calls
printoutfnorprintoutfnswhich is the silent version.To test the output I do it like this:
Notice how it includes the newline character:
\n.There are two invocations:
StdOut.call func paramandStdOut.run func paramthe former returns the function value and the output, the latter only returns the output