Tuesday, January 29, 2013

NBuilder simple example in C#

Nbuilder is a great tool in developing mock builders to generate data out of you application. Assuming, you don't use database for retrieving records, you can use NBuilder with LINQ capabilities to generate test data. Assuming if you have a class in your model like this:
namespace MvcApplication1.Models  
   public class Product  
     public int Id { get; set; }  
     public string Name { get; set; }  
     public double Price { get; set; }  
     public string Description { get; set; }  
To generate a product object using NBuilder, you may use something like this:
Product product = Builder<Product>  
         .With(x => x.ProductId = productId)  
         .And(x => x.Price = Convert.ToDouble(productId))  
         .And(x => x.Name = "Mushroom Can")  
         .And(x => x.Description = @"Imported from New Zealand. Fresh and Juicy.")  


Post a Comment