Book Image

Testing with F#

By : Mikael Lundin
Book Image

Testing with F#

By: Mikael Lundin

Overview of this book

Table of Contents (17 chapters)
Testing with F#
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Dependency injection


What we're used to from object-oriented systems is that dependencies are injected into the classes where they're used. There are a couple of different kinds of injections, namely:

  • Constructor injection

  • Property injection

  • Method injection

Constructor injection is by far the most common injection type in object-oriented programming. This is, of course, in order to make the necessary dependencies mandatory and not have to check if they're set or not.

Here is what constructor injection would look like in F#:

type CustomerRepository (csvReader : ICsvReader) =
    member this.Load filePath = 
        csvReader.Load filePath 
            |> Seq.map schema
            |> Seq.toList

The dependency, which here is the ICsvReader, is received in the constructor of the CustomerRepository type.

The way to test this is pretty obvious; by creating a test double for the ICsvReader, as seen before, and injecting it into the constructor:

[<Test>]
let ``should parse ID as int`` () =...