NBuilder type padding types

I use nBuilder to populate an object graph, but it only populates value types. I want it to populate reference types (related objects).

http://nbuilder.org/

+4
source share
3 answers

NBuilder does not currently support automatic filling of reference types.

However, you can do what you want by using the constructor to create each reference type.

You are probably doing this at the moment:

var person = Builder<Person> .CreateNew() .Build(); Assert.That(person.Name, Is.EqualTo("Name1")); Assert.That(person.Address, Is.Null); 

What you want to do is:

 var address = Builder<Address> .CreateNew() .Build(); var person2 = Builder<Person> .CreateNew() .With(x => x.Address = address) .Build(); Assert.That(person2.Name, Is.EqualTo("Name1")); Assert.That(person2.Address, Is.Not.Null); Assert.That(person2.Address.Street, Is.EqualTo("Street1")); Assert.That(person2.Address.Zipcode, Is.EqualTo("Zipcode1")); 
+4
source

The limitation that I found with NBuilder is that the data it generates for strings in this way is that it is based on property names, for example. Name1, Street1, Zipcode1, as you see above. I found that I use .Phrase (), but it does not generate reasonable random data, and elements like emails should be put together.

You can download Faker.Net via the nuget link here or use Visual Studio and create simulated data as part of your build team. Then you can use it to create your personal objects (again using Faker / NBuilder).

 var addresses = Builder<Address>.CreateListOfSize(20) .All() .With(c => c.Street = Faker.StreetName().First()) .With(c => c.State = Faker.UsState().First()) .With(c => c.ZipCode = Faker.ZipCode().First()) .Build(); 

This blog post provides some more examples.

+1
source

This is not possible in NBuilder.

Although there is a hand tool . This article contains a code snippet that recursively calls NBuilder to create objects that fill the reference and collection properties of the root object (to the specified depth):

 var recursiveObjectBuilder = new RecursiveObjectBuilder(graphDepth: 2, listSize: 3); var complexObject = recursiveObjectBuilder.CreateGenericObject<ComplexType>(recursive:true); Assert.NotNull(complexObject.ReferenceToOtherObject); int someValue = complexObject.ReferenceToOtherObject.SomeValue; 
0
source

All Articles