-
Notifications
You must be signed in to change notification settings - Fork 337
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();
var autoGeneratedText = fixture.Create<string>();
string: "f5cdf6b1-a473-410f-95f3-f427f7abb0c7"
http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx
Install AutoFixture.SeedExtensions
NuGet package to access overloads with a seed.
var generatedTextWithPrefix = fixture.Create("Name");
string: "Name30a35da1-d681-441b-9db3-77ff51728b58"
http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx
int autoGeneratedNumber = fixture.Create<int>();
int: 27, followed by 9, then by 171, etc.
https://github.com/AutoFixture/AutoFixture/wiki/v3.0-Release-Notes#numbers-are-random
var autoGeneratedClass =
fixture.Create<ComplexParent>();
ComplexParent:
- Child: ComplexChild
- Name: string: "namef70b67ff-05d3-4498-95c9-de74e1aa0c3c"
- Number: int: 1
http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx
fixture.Register<IMyInterface>(() =>
new FakeMyInterface());
Every time the fixture instance is asked to create an instance of IMyInterface, it will return a new instance of FakeMyInterface.
http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx
fixture.Register<string>(() => "ploeh");
string result = fixture.Create<string>();
string: "ploeh"
http://blog.ploeh.dk/2009/04/27/ReplacingAutoFixturesDefaultAlgorithms.aspx
var strings = fixture.CreateMany<string>();
IEnumerable<string>
:
- string: "ecc1cc75-cd7a-417f-b477-2913802440b4"
- string: "fce70a7b-fae5-474f-8055-415ca46eac20"
- string: "79b45532-d66f-4abc-9311-77ba68dc9e3c"
http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx
var myInstances = fixture.CreateMany<MyClass>();
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"
http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx
var list = new List<MyClass>();
fixture.AddManyTo(list);
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"
http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx
var mc = fixture.Build<MyClass>()
.With(x => x.MyText, "Ploeh")
.Create();
MyClass:
- MyText: string: "Ploeh"
var sut = fixture.Build<Vehicle>()
.OmitAutoProperties()
.Create();
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
http://blog.ploeh.dk/2009/07/23/DisablingAutoPropertiesInAutoFixture.aspx
var person = fixture.Build<Person>()
.Without(p => p.Spouse)
.Create();
Person:
- BirthDay: DateTime: {18.08.2009 07:37:06}
- Name: String: "Name949c7c83-c77b-434f-a8fe-e0aa73f81fbe"
- Spouse: Person: null
http://blog.ploeh.dk/2009/08/17/OmittingOnlyCertainPropertiesWithAutoFixture.aspx
var mc = fixture.Create<MyClass>();
var mvm = fixture.Build<MyViewModel>()
.Do(x => x.AvailableItems.Add(mc))
.With(x => x.SelectedItem, mc)
.Create();
MyViewModel:
- AvailableItems:
ICollection<MyClass>
- MyClass (mc)
- SelectedItem: MyClass (mc)
http://blog.ploeh.dk/2009/08/25/DoRedux.aspx
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>();
MyViewModel:
- AvailableItems:
ICollection<MyClass>
- MyClass (mc)
- SelectedItem: MyClass (mc)
http://blog.ploeh.dk/2009/09/22/CustomizingATypesBuilderWithAutoFixture.aspx
Install AutoFixture.NUnit*
or AutoFixture.xUnit*
NuGet package.
[Theory, AutoData]
public void Test(int primitiveValue, string text)
{
}
primitiveValue: int: 1
text: string: "textf70b67ff-05d3-4498-95c9-de74e1aa0c3c"
http://blog.ploeh.dk/2010/10/08/AutoDataTheoriesWithAutoFixture.aspx
Install AutoFixture.NUnit*
or AutoFixture.xUnit*
NuGet package.
[Theory]
[InlineAutoData("foo")]
[InlineAutoData("foo", "bar")]
public void Test(string text1, string text2, MyClass myClass)
{
}
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
http://www.nikosbaxevanis.com/bonus-bits/2011/08/combining-xunit-data-theories.html
Install AutoFixture.AutoMoq
NuGet package.
fixture.Customize(new AutoMoqCustomization());
var result = fixture.Create<IInterface>();
A mocked instance of a type assignable from IInterface.
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.
- 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