Unit Testing
There are 2 entries for the tag Unit Testing
Friday, January 22, 2010 3:53 PM | Comments

Unit testing a method that relies on DateTime.Now is generally a challenge. Take the below example, which was the first version of a method to get the Current Price of a Product. The business rule for GetCurrentPrice() is simple: find the most recent price where the effective date is not in the future. For this scenario, imagine the user sets the Price of a product to $25,000 but that price doesn't take effect until the next year. Until that time, we must make sure that the system returns the previous (existing) price. The poorly testable method public decimal GetCurrentPrice() { var priceHistory = PriceHistories .Where(ph.EffectiveDate.Date <= DateTime.Now.Date) .OrderByDescending(ph => ph.EffectiveDate) .Take(1) .SingleOrDefault(); return (priceHistory != null) ? priceHistory.Price...

Tuesday, September 8, 2009 10:59 PM | Comments

Today I began integration testing my SqlRepository while I experiment with an MVC prototype. I tried googling for a simple base class for integration testing a LINQ to SQL DataContext that would provide automatic rollback between each test, but after (minimal) searching, decided to simply write my own. Perhaps this will help someone. This base class was written for MS Test, but simply renaming the attributes accordingly would yield the same results on nUnit, xUnit, etc. The Code Simply inherit your test class from this base class and any changes done via the internal Context property will be automatically rolled back after each test runs. /// <summary> /// The base class will allow LINQ to SQL integration testing with automatic rollbacks after each test /// </summary> /// <typeparam name="TContext">The type of data context to use</typeparam> public abstract class RolledBackDataContextTests<TContext> where TContext : DataContext { private TContext _context; ...