Persistence

NBuilder also has another method called Persist(). It does the same thing as Build() but once the objects have been built it will call a predefined method to persist the objects somewhere.

The first thing you need to do is tell NBuilder how to persist objects of a certain type. You do this with the BuilderSetup class. (You can read more about BuilderSetup in the configuration section).

    
BuilderSetup.SetCreatePersistenceMethod<Product>(productRepository.Create);
BuilderSetup.SetCreatePersistenceMethod<IList<Product>>(productRepository.CreateAll);

You only need to do this once per test run. Once this is done you simply call Persist() instead.

    
Builder<Product>.CreateNew().Persist();