-
Notifications
You must be signed in to change notification settings - Fork 0
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
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: 1, followed by 2, then by 3, etc.
http://blog.ploeh.dk/2009/04/03/CreatingNumbersWithAutoFixture.aspx
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.TypeMappings[typeof(string)] = s => "fnaah";
string result = fixture.Create<string>();
string: "fnaah"
http://blog.ploeh.dk/2009/04/27/ReplacingAutoFixturesDefaultAlgorithms.aspx
var strings = fixture.CreateMany<string>();
IEnumerable:
- 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:
- 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:
- 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 (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 (mc)
- SelectedItem: MyClass (mc)
http://blog.ploeh.dk/2009/09/22/CustomizingATypesBuilderWithAutoFixture.aspx
Add a reference to assembly Ploeh.AutoFixture.Xunit.
[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
Add a reference to assembly Ploeh.AutoFixture.Xunit.
[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
Add a reference to assembly Ploeh.AutoFixture.AutoMoq.
fixture.Customize(new AutoMoqCustomization());
var result = fixture.Create<IInterface>();
A mocked instance of a type assignable from IInterface.
Note that AutoMoqCustomization overrides the default behavior for collections (List/Array/IEnumerable<T>), though one can 'reinstate' that by adding a MultipleCustomization in front of it as discussed in the Ordering article)
http://blog.ploeh.dk/2010/08/19/AutoFixtureAsAnAutomockingContainer.aspx