O'Reilly logo

Testing with F# by Mikael Lundin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required