Project Description
Foq is a lightweight thread-safe mocking library for F#, C# & VB.Net. Use Foq to mock abstract classes and interfaces.

Foq is available on Nuget:
Or simply include Foq.fs in your project or reference it from an F# script.
It's also bundled with AutoFixture.AutoFoq

#load "foq.fs"
open Foq

// Mock methods
let mock =
    .Setup(fun x -> <@ x.Contains(any()) @>).Returns(true)

// Mock properties
let mock =
    .Setup(fun x -> <@ x.Count @>).Returns(1)
mock.Count = 1

// Mock events
let event = Event<_,_>()
let mock =
    .SetupEvent(fun x -> <@ x.PropertyChanged @>).Publishes(event.Publish)
let triggered = ref false
mock.PropertyChanged.Add(fun x -> triggered := true)
event.Trigger(mock, System.ComponentModel.PropertyChangedEventArgs("PropertyName"))

// Match method arguments with predicates
let mock =
    .Setup(fun x -> <@ x.Remove(is(fun i -> i >= 0)) @>).Returns(true)
    .Setup(fun x -> <@ x.Remove(is(fun i -> i <  0)) @>).Raises<ArgumentOutOfRangeException>()
See more usage exampes in ShopExample.fs, Usage.fsx, Usage.Linq.fsx or LinqTests.cs.

Foq provides a familiar fluent interface for Moq users with strong typing via: Foq is thread-safe.

Foq depends on FSharp.Core.dll which ships with Xamarin Studio on Mac and PC and in full fat Visual Studio for Windows.
You can also use F# in Visual Studio Express 2012 with the F# Tools, which can also be installed on a build server.
For MonoDevelop you may need to install the F# Language Bindings via the Add-In Manager.
If you are using .Net 4 rather than the latest Net 4.5 in Visual Studio 2012 you may need to add a bindingRedirect to your project's app.config file (the latest Nuget package should do this for you automatically). If you are using .Net 4.5.1 or F# 3.1 in Visual Studio 2013 you can enable automatic binding redirection.

Contributions are welcome, particularly code samples, articles and documentation. Please add feature requests to the Issues list or get in touch via Twitter if you'd like to contribute to the project.

Last edited Feb 8, 2014 at 9:53 AM by ptrelford, version 45