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

Mocking


Even though Foq is calling itself a mocking framework, a mock is really a recorder of events on a dependency. You can use it to verify the interactions between parts in your system.

Let's say we have a situation where we want to synchronize customer data from a CRM system onto our local database. We could have interfaces as shown in the following code:

type Customer = { ID : int; Name : string }

type ICustomerService =
    abstract member GetCustomers : unit -> Customer list

type ICustomerDataAccess = 
    abstract member GetCustomer : int -> Customer option
    abstract member InsertCustomer : Customer -> unit
    abstract member UpdateCustomer : Customer -> unit

We write a simple scheduled job that will synchronize data nightly:

let synchronize (service : ICustomerService) (dataAccess : ICustomerDataAccess) =
    // get customers from service
    let customers = service.GetCustomers()

    // partition into inserts and updates
    let inserts, updates = 
        customers...