Skip to content

Instantly share code, notes, and snippets.

@JaykeOps
Last active January 5, 2018 10:22
Show Gist options
  • Save JaykeOps/905b1f8179c2664c0e3ff03aa6d02b5f to your computer and use it in GitHub Desktop.
Save JaykeOps/905b1f8179c2664c0e3ff03aa6d02b5f to your computer and use it in GitHub Desktop.
AutoFixture CheatSheet
var fixture = new Fixture();
//Primitives
var anonymousStr = fixture.Create<string>("This str makes it easier to read failed tests.");
var anonymousInt = fixture.Create<int>();
//Collections
var anonymousCollection = fixture.CreateMany<int>();
var limitedAnonymousCollection = fixture.CreateMany<string>(3);
//Add to collections
var collection = new Collection<string>();
var fixture.AddManyTo(collection, 10);
var fixture.AddManyTo(collection, () => "Hello World!"); //Will add Hello World! to the collection (3x is default)
//Non-primitive type creation
var car = fixture.Create<Car>(); //Create entires object graphs
var cars = fixture.CreateMany<Car>(100); //Create 100 cars with anonymous test data (.AddManyTo() also works)
//Customizing fixture data
fixture.Inject("1234"); //Same instance returned everytime fixture.Create<string>() is called.
fixture.Inject(new Car("M3", "BMW", 2015)); //Same instance returned everytime fixture.Create<Car>() is called.
fixture.Register(() => "Lada"); //New instance returned everytime fixture.Create<string>() is called.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment