Skip to content

Repository files navigation

SimpleFixture

SimpleFixture is a .net library that helps provide test data for unit tests and integration tests. The fixture satisfies these three basic use cases. It supports .Net Standard 1.0, and PCL Profile 259.

  • Locate - Creates a new instance of the requested type. It does not populate public properies.
varfixture=newFixture();varinstance=fixture.Locate<SomeClass>();
  • Populate - Populate all public properties with randomly created data for a given instance
varfixture=newFixture();fixture.Populate(instance);
  • Generate - Create a new instance of a given type and populate all public properties.
varfixture=newFixture();varinstance=fixture.Generate<SomeClass>();

Return

It's useful sometimes to control what values are returned from the fixture. The Return method offers a way to specify what to return for a give type. Below are some examples.

varfixture=newFixture();// return the value 1 whenever an int is neededfixture.Return(1);// return the sequence 1, 2, 3, 1, 2, 3 when an int is requested fixture.Return(1,2,3);// return incrementing sequence when an int is requested.inti=1;fixture.Return(()=>i++);

Return also offers the ability to be more granular when returning as seen in the examples below. Note you can chain return filters together to be even more granular

varfixture=newFixture();// return the value 1 when being used to construct SomeClassfixture.Return(1).For<SomeClass>();// return incrementing value for int fields ending in Idinti=1;fixture.Return(()=>i++).WhenNamed(n =>n.EndsWith("Id"));// return "SomeString" for SomeClass and matchingMethod returns truefixture.Return("SomeString").For<SomeClass>().WhenMatching(matchingMethod);

Export

Similar to a depenedency injection container you can specify an implementation for a particular interface

varfixture=newFixture();// Export one class at a timefixture.ExportAs<SomeClass,ISomeInterface>();varinstance=fixture.Locate<ISomeInterface>();Assert.InstanceOf<SomeClass>(instance);// Export all classes located in the same assmelby as SomeClassfixture.ExportAllByInterface().FromAssemblyContaining<SomeClass>();varinstance=fixture.Locate<ISomeInterface>();Assert.InstanceOf<SomeClass>(instance);

Constraints

Sometimes it's useful to have more control over how an object is created. The generate method takes a constraints object allowing to specify min,max, and other options.

// Generate an int between 10 and 100varintValue=fixture.Generate<int>(constraints:new{min=10,max=100});// Generate constrained datevardateValue=fixture.Generate<DateTime>(constraints:new{min=dateMin,max=dateMax});// Set property SomeProperty to value 123 for any int property named SomePropertyvarinstance=fixture.Generate<SomeClass>(constraints:new{SomeProperty=123});// _Values allows you to provide values based on type rather than on namevarsomeClass=newSomeClass{IntValue=50,StringValue="Test"};varinstance=fixture.Generate<ImportSomeClass>(constraints:new{_Values=new[]{someClass}});Assert.Same(someClass,instance.SomeClass);

Customization

Customization offers you the ability to control how an object gets constructed and populated when Return and Constraints aren't enough.

varfixture=newFixture();// customize how a new instance is createdfixture.Customize<SomeClass>().New(r =>newSomeClass());// customize creation for a new instance with a random int and stringfixture.Customize<SomeOtherClass>().NewFactory<int,string>((i,s)=newSomeOtherClass(i,s,"HardCoded"));// call the Initialize method everytime a new instance is createdfixture.Customize<SomeClass>().Apply(x =>x.Initialize());

Requested Name

To help provide more context on how a type should be create you can provide a request name. Below are some example of currently supported request names for strings

varfixture=newFixture();// generate a first namevarfirstName=fixture.Generate<string>("FirstName");// generate a last namevarlastName=fixture.Generate<string>("LastName");// generate a random password with 1 Upper, 1 Lower, 1 Special character and a minimium of 8 charactersvarpassword=fixture.Generate<string>("Password");// generate a random email for the specified domainvaremail=fixture.Generate<string>("EmailAddress",constraints:new{domain="gmail.com"});// generate an address line 1varaddressLine1=fixture.Generate<string>("AddressLine1");

Freeze

Similar to Autofixture there is a Freeze method that Generates a new instance and sets it as a Return value.

// Generate random int and set it as a ReturnintrandomInt=fixture.Freeze<int>();Assert.Equal(randomInt,fixture.Generate<int>());// Freeze an int value for SomeClassintrandomInt=fixture.Freeze<int>(value: i =>i.For<SomeClass>());

Behavior

Behavior allows you to apply cross cutting logic to all objects created by the fixture. You can apply your logic to all objects or just to specific types.

// Execute SomeMethod on every object that is created by the fixturefixture.Behavior.Add(i =>SomeMethod(i));// Execute behavior on every instance of ISomeTypefixture.Behavior.Add<ISomeType>(i =>i.SomeMethodOnISomeType());

Mocking

Currently Moq, NSubstitute and FakeItEasy are supported allowing you to automatically mock any missing interfaces

// MoqFixture is in SimpleFixture.Moqvarfixture=newMoqFixture();// SubFixture is in SimpleFixture.NSubstitutevarfixture=newSubFixture();// FakeFixture is in SimpleFixture.FakeItEasyvarfixture=newFakeFixture();

Builds

Build statusCoverage Status

About

Testing fixture for .Net

Topics

Resources

Stars

11 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages