Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unit test private methods in F#

Let's say we have a class

type ThisClassIsComplicated () = 
    let calculateSomething a b =
        a + b 

In this case calculateSomething is trivial, but if it would be more complicated it may make sense to verify that the calculations done there are correct.

It might make sense to use a unit testing framework to test that private methods.

My question: how to unit test private methods in F#?

Some random thoughts:

The selected answer here, suggests to use the InternalsVisibleTo attribute which anyway is applicable only to internalmethods.

What is the route specific to F# if any? Is this better in a F# design?

let calculateSomething a b = a + b 

type ThisClassIsComplicated () = 
    member this.Calculate a b = calculateSomething a b

Maybe the scope of calculateSomething could be even narrowed down by having a nested module.

like image 596
NoIdeaHowToFixThis Avatar asked Dec 26 '22 11:12

NoIdeaHowToFixThis


1 Answers

If you feel like your code is too complicated to test it from the outside, use the latter option. And in case you want to test an inner function like

let myComplicatedOperation input = 
    let calculateSomething a b =
        a + b
    calculateSomething (fst input) (snd input)

you can always rewrite it with currying like this:

let myComplicatedOperation calculateSomething input =
    calculateSomething (fst input) (snd input)

Your question does not seem to be directly related to F# though. The general way to test private methods is typically by extracting a class (or, in F#, you can also just extract a let bound function). And making your testee public on that other class / function.

like image 153
Daniel Fabian Avatar answered Dec 28 '22 09:12

Daniel Fabian