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

Test doubles


The definition of a test double is a dependency you inject into your program, instead of ordinary functionality, in order to isolate the thing you want to test. We've already seen what you can do by making in-memory versions of your dependencies built in. Sometimes, we want our tests to be able to inject a specific tests dependency that is not built in, and this is where the SUT needs to be extensible enough to allow it.

Just as in C#, we use an interface or abstract class to enable extensibility and allow for dependency injection. To enable this, F# provides us with the ability to implement interfaces using object expressions. This is a great feature for testing.

As an example, lets start with defining an interface. This interface will open a Comma Separated Value (CSV) file and allow reading it line by line, as shown in the following code:

// loading and processing a csv file line by line
type ICsvReader =
    // open file and return sequence of values per line
    abstract member...