Search code examples
c#nbuilder

nBuilder only populating value types


I am using nBuilder to populate an object graph, but it is only populating the value types. I want it to populate the reference types (related objects).

http://nbuilder.org/


Solution

  • NBuilder doesn't support automatically populating reference types at this time.

    However, it is possible to do what you're wanting by using a builder to create each reference type.

    At the moment you are probably doing this:

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

    What you want to be doing is this:

    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"));