AutoObjectBuilder.Net is my latest open source project designed to assist with Unit testing by automatically building simple and complex object graphs with predictable test data.
Now available on NuGet.
PM> Install-Package AutoObjectBuilder
- Recursive instance creation - injecting constructor dependencies as required
- Able to create internal, protected and constructor-less instances
- Auto generation of interface/abstract class proxies
- Provides instances recursively populated with predicable test data
- Provides a fluent interface for overriding default construction behaviour to cover different edge cases
- Auto populates ICollection, IList, IDictionary, and Array types with a configurable number of items
- Enables use of custom configuration via extension methods.
The library can be used to automatically create and build objects recursively, populating them with predicable test data as they are constructed. These instances can then be used to compliment other Mocking frameworks, by allowing them to be injected as Mocked return values. This avoids bloating unit tests with extensive set-up code for complex objects.
The following simplistic example shows how the library might be used with the Moq framework to return an auto-generated instance pre-populated with test values. The test takes advantage of the fact that the Username/Password members of the generated instance are predicable, and therefore is able to validate the application logic within the UserService Login method.
IPerson person = Auto.Make<IPerson>().Object; var moq = new Mock<IUserRespository>(); var moq.SetUp(o => o.FindUser(It.IsAny<int>())).Return(person); UserService service = new UserService(moq.Object); var loggedInUser = service.Login("Username", "Password"); Assert.That(loggedInUser, Is.EqualTo(person)); Assert.That(loggedInUser.Username, Is.EqualTo("Username")); Assert.That(loggedInUser.Password, Is.EqualTo("Password"));