F# UnitTesting function with side effect

Viewed 348

I am C# dev that has just starting to learn F# and I have a few questions about unit testing. Let's say I want to the following code:

let input () = Console.In.ReadLine()

type MyType= {Name:string; Coordinate:Coordinate}

let readMyType = 
   input().Split(';') 
   |> fun x -> {Name=x.[1]; Coordinate = {
   Longitude = float(x.[4].Replace(",",".")) 
   Latitude =float(x.[5].Replace(",","."))
   }}

As you can notice, there are a few points to take in consideration:

  • readMyType is calling input() with has a side effect.
  • readMyType assume many thing on the string read (contains ';' at least 6 columns, some columns are float with ',')

I think the way of doing this would be to:

  • inject the input() func as parameter
  • try to test what we are getting (pattern matching?)
  • Using NUnit as explained here

To be honest I'm just struggling to find an example that is showing me this, in order to learn the syntax and other best practices in F#. So if you could show me the path that would be very great.

Thanks in advance.

2 Answers
Related