WriteBetterTests.FsCheckSpy
1.0.0
dotnet add package WriteBetterTests.FsCheckSpy --version 1.0.0
NuGet\Install-Package WriteBetterTests.FsCheckSpy -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WriteBetterTests.FsCheckSpy" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WriteBetterTests.FsCheckSpy" Version="1.0.0" />
<PackageReference Include="WriteBetterTests.FsCheckSpy" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WriteBetterTests.FsCheckSpy --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: WriteBetterTests.FsCheckSpy, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package WriteBetterTests.FsCheckSpy@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=WriteBetterTests.FsCheckSpy&version=1.0.0
#tool nuget:?package=WriteBetterTests.FsCheckSpy&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
WriteBetterTests.FsCheckSpy
FsCheckSpy adds spies to your FsCheck tests.
- F# Site → docs/fsharp/index.html
- C# Site → docs/csharp/index.html
API docs:
- F# API → docs/fsharp/reference/index.html
- C# API → docs/csharp/api/index.html
Installation
dotnet add package WriteBetterTests.FsCheckSpy
Quickstart
Create a spy. Run the property. Read the log.
- F#
open FsCheck
open FsCheck.FSharp
open FsCheckSpy
open Swensen.Unquote
type Calculator = { add: int -> int -> int }
with
static member Create (add) = { add = add }
type CalculatorLog = { add: CallLog<(int * int), int> }
with
static member Create (add) = { add = add }
let arbCalculator () =
Spy.fn<(int * int), int>()
.MapFn(fun fn -> fun x -> fun y -> fn(x, y))
.Arbitrary(CalculatorLog.Create, Calculator.Create).Arb
[<Property>]
let prop_Calculator () =
Prop.forAll (Arb.fromGen(Gen.zip3 (arbCalculator().Generator) (Gen.choose((1, 100))) (Gen.choose(1, 100)))) <| fun ((log, calc), x, y) ->
// Use the calculator - we don't know what it returns
let result = calc.add x y
// But we can verify it was called correctly
<@ log.add[0].Args = (x, y) @> |> ignore
// And we can verify the result
<@ log.add[0].Result = result @> |> ignore
- C#
using FsCheck;
using FsCheckSpy;
public readonly record struct Calculator(Func<int, int, int> Add)
{
public static Calculator Create(Func<int, int, int> args) => new(args);
}
public readonly record struct CalculatorLogs(CallLog<(int, int), int> Add)
{
public static CalculatorLogs Create(CallLog<(int, int), int> args) => new(args);
}
var arb = Spy.Fn<(int, int), int>()
.InvokeMapFn<Func<int, int, int>>(f => (int x, int y) => f((x, y)))
.ToArbitrary(CalculatorLogs.Create, Calculator.Create)
.Arb;
Prop.ForAll(Arb.From(arb), pair =>
{
var (logs, calc) = pair;
var x = 3; var y = 5;
var result = calc.Add(x, y);
return logs.Add[0].Args == (x, y)
&& logs.Add[0].Result == result;
}).QuickCheckThrowOnFailure();
Anatomy of a Spy
- You get two things: a log and a function (your dependency).
- Each call to the function adds one entry to the log with
ArgsandResult. - Async logs store success or error:
Result<'T, exn>(F#) orSpyResult<T,E>(C#). - Spies can be composed to build concrete types that match your program dependencies.
Learn more
- F# Guide → docs/fsharp.md
- C# Guide → docs/csharp.md
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- FsCheck (>= 3.3.1)
- FSharp.Core (>= 9.0.303)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 222 | 9/27/2025 |