Document your code
Every project on GitHub comes with a version-controlled wiki to give your documentation the high level of care it deserves. It’s easy to create well-maintained, Markdown or rich text documentation alongside your code.
Sign up for free See pricing for teams and enterprisesCheat 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 Autogenerated String
var autoGeneratedText = fixture.Create<string>();
Sample Result
string: "f5cdf6b1-a473-410f-95f3-f427f7abb0c7"
More Information
http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx
Seeded String
Install AutoFixture.SeedExtensions
NuGet package to access overloads with a seed.
var generatedTextWithPrefix = fixture.Create("Name");
Sample Result
string: "Name30a35da1-d681-441b-9db3-77ff51728b58"
More Information
http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx
Autogenerated Number
int autoGeneratedNumber = fixture.Create<int>();
Sample Result
int: 27, followed by 9, then by 171, etc.
More Information
https://github.com/AutoFixture/AutoFixture/wiki/Version-History#numbers-are-random
Complex Type
var autoGeneratedClass =
fixture.Create<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.Register<string>(() => "ploeh");
string result = fixture.Create<string>();
Sample Result
string: "ploeh"
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")
.Create();
Sample Result
MyClass:
- MyText: string: "Ploeh"
More Information
Disable AutoProperties
var sut = fixture.Build<Vehicle>()
.OmitAutoProperties()
.Create();
Sample Result
Vehicle:
- Wheels: int: 4
The Wheels property will have the default value of 4, instead of having an auto generated 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)
.Create();
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.Create<MyClass>();
var mvm = fixture.Build<MyViewModel>()
.Do(x => x.AvailableItems.Add(mc))
.With(x => x.SelectedItem, mc)
.Create();
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.Create<MyClass>();
fixture.Customize<MyViewModel>(
ob => ob.Do(x => x.AvailableItems.Add(mc))
.With(x => x.SelectedItem, mc));
var mvm = fixture.Create<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
Install AutoFixture.NUnit*
or AutoFixture.xUnit*
NuGet package.
[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
Install AutoFixture.NUnit*
or AutoFixture.xUnit*
NuGet package.
[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 autogenerated variable of type MyClass
Second test run:
text1: string: "foo"
text2: string: "bar"
myClass: an autogenerated variable of type MyClass
More Information
http://www.nikosbaxevanis.com/bonus-bits/2011/08/combining-xunit-data-theories.html
Auto-Mocking with Moq
Install AutoFixture.AutoMoq
NuGet package.
fixture.Customize(new AutoMoqCustomization());
var result = fixture.Create<IInterface>();
Sample Result
A mocked instance of a type assignable from IInterface.
Auto-configured Mocks
When new AutoMoqCustomization { ConfigureMembers = true }
is added to an IFixture
instance, not only will it behave as an Auto-Mocking Container, but it will also automatically configure all the generated Test Doubles (Mocks) so that their members return values generated by AutoFixture.
fixture.Customize(new AutoMoqCustomization { ConfigureMembers = true });
fixture.Inject<int>(1234);
var document = fixture.Create<IDocument>();
Console.WriteLine(document.Id); //1234
This customization will automatically configure any virtual methods/indexers/out parameters and stub all properties. Additionally, class mocks will have their fields set.
More Information
- Due to a limitation in Moq, auto-configured mode is not able to setup methods with
ref
parameters. - Auto-configured mode does not configure generic methods either. You can, however, easily set these up using the
ReturnsUsingFixture
extension method:
converter.Setup(x => x.Convert<double>("10.0"))
.ReturnsUsingFixture(fixture);
http://blog.ploeh.dk/2010/08/19/AutoFixtureAsAnAutomockingContainer.aspx