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

Don't mock the Mockingbird


Let us take a look at the following code:

// don't
[<Test>]
let ``should first get customers from customer service and then store them to hard drive`` () =
    // arrange
    let customerService = MockRepository.GenerateMock<ICustomerService>()
    let fileSystem = MockRepository.GenerateMock<IFileSystem>()
    let cacheJob = CacheJob(customerService, fileSystem)

    // setup mocks
    customerService.Expect(fun service -> service.GetCustomers()).Return([(1, "Mikael Lundin")]) |> ignore
    fileSystem.Expect(fun fs -> fs.AppendLineToFile("customer.txt", "1,Mikael Lundin")) |> ignore

    // act
    cacheJob.Execute() |> ignore
        
    // assert
    customerService.VerifyAllExpectations()
    fileSystem.VerifyAllExpectations()

// do
// simplify the SUT or implement a vertical slice

What if we change the storage from filesystem to database? Should the test fail when implementation changes?

Mocks aren't evil, but they are very...