AutoFixture Cheat Sheet
This page contains short code snippets that demonstrate AutoFixture features. All examples assume that a Fixture instance called
fixture has previously been created like this:
var fixture = new Fixture();
Completely Anonymous String
var anonymousText = fixture.CreateAnonymous<string>();
Sample Result
string: "f5cdf6b1-a473-410f-95f3-f427f7abb0c7"
More Information
http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx
Seeded String
var anonymousName = fixture.CreateAnonymous("Name");
Sample Result
string: "Name30a35da1-d681-441b-9db3-77ff51728b58"
More Information
http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx
Anonymous Number
int anonymousNumber = fixture.CreateAnonymous<int>();
Sample Result
int: 1, followed by 2, then by 3, etc.
More Information
http://blog.ploeh.dk/2009/04/03/CreatingNumbersWithAutoFixture.aspx
Complex Type
var anonymousParent =
fixture.CreateAnonymous<ComplexParent>();
Sample Result
ComplexParent:
->Child: ComplexChild
-->Name: string: "namef70b67ff-05d3-4498-95c9-de74e1aa0c3c"
-->Number: int: 1
More Information
http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx
Abstract Types
fixture.Register<IMyInterface>(() =>
new FakeMyInterface());
Sample Result
Every time the
fixture instance is asked to create an instance of IMyInterface, it will return a new instance of FakeMyInterface.
More Information
http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx
Replaced Default Algorithm
fixture.TypeMappings[typeof(string)] = s => "fnaah";
string result = fixture.CreateAnonymous<string>();
Sample Result
string: "fnaah"
More Information
http://blog.ploeh.dk/2009/04/27/ReplacingAutoFixturesDefaultAlgorithms.aspx
Sequence of Strings
var strings = fixture.CreateMany<string>();
Sample Result
IEnumerable<string>:
-> string: "ecc1cc75-cd7a-417f-b477-2913802440b4"
-> string: "fce70a7b-fae5-474f-8055-415ca46eac20"
-> string: "79b45532-d66f-4abc-9311-77ba68dc9e3c"
More Information
http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx
Sequence of Custom Objects
var myInstances = fixture.CreateMany<MyClass>();
Sample Result
IEnumerable<MyClass>:
-> MyClass:
--> MyText: string: "MyTextfda10499-e112-476b-924a-2c7b831227f2"
-> MyClass:
--> MyText: string: "MyText6140d5f8-0639-4718-a82b-181d0410f9cf"
-> MyClass:
--> MyText: string: "MyText4a89c288-694a-4a19-a407-7348b70420cf"
More Information
http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx
Add to Collection
var list = new List<MyClass>();
fixture.AddManyTo(list);
Sample Result
List<MyClass>:
-> MyClass:
--> MyText: string: "MyTextca86de74-e8df-46b1-bc15-63f763ce9e07"
-> MyClass:
--> MyText: string: "MyTextc45ff7b9-b30e-4246-b535-2eb06bc888c0"
-> MyClass:
--> MyText: string: "MyTextefadfab3-0992-4ecb-a3df-c6f1d5e61f12"
More Information
http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx
Set Property
var mc = fixture.Build<MyClass>()
.With(x => x.MyText, "Ploeh")
.CreateAnonymous();
Sample Result
MyClass:
-> MyText: string: "Ploeh"
More Information
http://blog.ploeh.dk/2009/06/01/SettingPropertyValuesWhileBuildingAnonymousVariablesWithAutoFixture.aspx
Disable AutoProperties
var sut = fixture.Build<Vehicle>()
.OmitAutoProperties()
.CreateAnonymous();
Sample Result
Vehicle:
-> Wheels: int: 4
The Wheels property will have the default value of 4, instead of having an anonymous value assigned via its setter
More Information
http://blog.ploeh.dk/2009/07/23/DisablingAutoPropertiesInAutoFixture.aspx
Disable Property
var person = fixture.Build<Person>()
.Without(p => p.Spouse)
.CreateAnonymous();
Sample Result
Person:
-> BirthDay: DateTime: {18.08.2009 07:37:06}
-> Name: String: "Name949c7c83-c77b-434f-a8fe-e0aa73f81fbe"
-> Spouse: Person: null
More Information
http://blog.ploeh.dk/2009/08/17/OmittingOnlyCertainPropertiesWithAutoFixture.aspx
Perform Action
var mc = fixture.CreateAnonymous<MyClass>();
var mvm = fixture.Build<MyViewModel>()
.Do(x => x.AvailableItems.Add(mc))
.With(x => x.SelectedItem, mc)
.CreateAnonymous();
Sample Result
MyViewModel:
-> AvailableItems: ICollection<MyClass>
--> MyClass (mc)
-> SelectedItem: MyClass (mc)
More Information
http://blog.ploeh.dk/2009/08/25/DoRedux.aspx
Customize Type
var mc = fixture.CreateAnonymous<MyClass>();
fixture.Customize<MyViewModel>(ob => ob
.Do(x => x.AvailableItems.Add(mc))
.With(x => x.SelectedItem, mc));
var mvm = fixture.CreateAnonymous<MyViewModel>();
Sample Result
MyViewModel:
-> AvailableItems: ICollection<MyClass>
--> MyClass (mc)
-> SelectedItem: MyClass (mc)
More Information
http://blog.ploeh.dk/2009/09/22/CustomizingATypesBuilderWithAutoFixture.aspx
AutoData Theories
Add a reference to assembly
Ploeh.AutoFixture.Xunit.
[Theory, AutoData]
public void Test(int primitiveValue, string text)
{
}
Sample Result
primitiveValue: int: 1
text: string: "textf70b67ff-05d3-4498-95c9-de74e1aa0c3c"
More Information
http://blog.ploeh.dk/2010/10/08/AutoDataTheoriesWithAutoFixture.aspx
Inline AutoData Theories
Add a reference to assembly
Ploeh.AutoFixture.Xunit.
[Theory]
[InlineAutoData("foo")]
[InlineAutoData("foo", "bar")]
public void Test(string text1, string text2, MyClass myClass)
{
}
Sample Result
Uses the InlineData values for the the first method arguments, and then uses AutoData for the rest (when the InlineData values run out).
First test run:
text1: string: "foo"
text2: string: "text2c1528179-fd1b-4f5a-a1f3-636e91f8799e"
myClass: an anonymous variable of type MyClass
Second test run:
text1: string: "foo"
text2: string: "bar"
myClass: an anonymous variable of type MyClass
More Information
http://www.nikosbaxevanis.com/bonus-bits/2011/08/combining-xunit-data-theories.html
Auto-Mocking with Moq
Add a reference to assembly
Ploeh.AutoFixture.AutoMoq.
fixture.Customize(new AutoMoqCustomization());
var result = fixture.CreateAnonymous<IInterface>();
Sample Result
A mocked instance of a type assignable from IInterface.
More Information
http://blog.ploeh.dk/2010/08/19/AutoFixtureAsAnAutomockingContainer.aspx