To install BetaSoftwares.Fixture, you can either browse the Package Manager or run the following command in the Package Manager Console :
PM> Install-Package BetaSoftwares.Fixture
To start using **BetaSoftwares.Fixture**, you will first need to create an instance of the Fixture class :
using BetaSoftwares.Fixtures;
Fixture fixture = new Fixture();
To generate a new fixture of any type, use the Fixture.Create method :
string random = fixture.Create<string>();
// or..
string random = fixture.Create(typeof(string));
To generate many fixtures at the same time, you can use the Fixture.CreateMany method :
ICollection random = fixture.CreateMany<string>();
// or..
ICollection random = fixture.CreateMany(typeof(string));
To add many fixtures an already existing list, you can use the Fixture.AddManyTo method :
List<string> list = new List<string>();
// Add 5 fixture to the list
fixture.AddManyTo(list, 5);
int count = list.Count; // count = 5
You can also customize a fixture on creation using the Fixture.Build with the FixtureDefinition.With, FixtureDefinition.Without and FixtureDefinition.Do methods :
// With
ICollection random = fixture.Build<MyClass>().With(c => c.MyString, "Hello world").Create();
// Without
ICollection random = fixture.Build<MyClass>().Without(c => c.MyString).Create();
// Do
ICollection random = fixture.Build<MyClass>().Do(c => c.MyList.Clear()).Create();
// You can mix .With, .Without and .Do
ICollection random = fixture.Build<MyClass>().With(c => c.MyString, "Hello world").Do(c => c.MyList.Clear()).Create();
To register a type default value, you can use the Fixture.Register method :
// Registers a new default value
fixture.Register<IMyInterface>(() => new MyClass());
// The instance will be of the registered type
MyClass myClass = fixture.Create<IMyInterface>();
To customize a complex type default properties, you can use the Fixture.Customize method :
// Customize the class default properties
_fixture.Customize<ComplexType>(c => c.With(c => c.MyInteger, 42));
// The instance will have the custom properties
ComplexType myClass = fixture.Create<ComplexType>();
int value = myClass.MyInteger; // value = 42
Here is the index of currently supported types. If the type is not supported, the property will be set to the default value of the type.