Creating F# lists with AutoFixture
03 April 2016
This post explains how to customize AutoFixture in order to work with F# lists.
An exception is thrown when requesting F# lists with AutoFixture:
Lists, and other collection types in the System.Collections.Generic namespace, can be filled via the constructor that takes an
IEnumerable<T> as a parameter.
AutoFixture creates such instances via the constructor with the less number of parameters.
F# lists and the collection types in the System.Collections.Generic namespace are different. One of the differences is the way they’re created:
- F# lists can be built using the functions in the List module.
- Collection types in the System.Collections.Generic namespace can be built via the aforementioned constructor(s).
Customize AutoFixture to work with F# lists
The following customization can be applied to a
Fixture instance, in order to support the creation of F# lists:
The above can also be written declaratively using AutoData theories. Here’s an example for xUnit.net:
- The fact that AutoFixture can’t currently create F# lists out of the box was put to my attention by Rune Ibsen.
- The customization in this post is based on a similar one that Rune Ibsen showed me.
ReflectiveList code is adapted from this snippet by Rick Minerich.