NUnit Mock with WinForms Code Review

Define the Object Test Fixture Class

Code Review

Code Walkthrough

* Based on NUnit version 2.4.8

Define the Test class, which contains all the unit test code to be run against the Basket and BasketItem classes.

[TestFixture] public class TestBasketItem { private DynamicMock m_dataAccessProvider = null;
[TestFixtureSetUp] public void FixtureInitialise() { Console.WriteLine("FixtureInitialise"); }
[SetUp] public void TestInitialise() { this.m_dataAccessProvider = new DynamicMock(typeof(IShoppingDataAccess)); this.m_dataAccessProvider.Strict = false; Console.WriteLine("TestInitialise: {0}", (this.m_dataAccessProvider == null)); }
[Test] public void TestSetReturnValueOfUnitPriceAndProductName() { this.m_dataAccessProvider.SetReturnValue("GetUnitPrice", Convert.ToInt32(99)); this.m_dataAccessProvider.SetReturnValue("GetProductName", "The Moon");
Console.WriteLine("TestSetReturnValueOfUnitPriceAndProductName: {0}", (this.m_dataAccessProvider == null));
BasketItem item = new BasketItem(1, 2, (IShoppingDataAccess)this.m_dataAccessProvider.MockInstance); Assert.That(99, Is.EqualTo(item.UnitPrice)); Assert.That("The Moon", Is.EqualTo(item.ProductName)); Assert.That(198, Is.EqualTo(item.GetPrice()));
this.m_dataAccessProvider.Verify(); }
[Test] public void TestExpectAndReturnOfUnitPriceAndProductName() { this.m_dataAccessProvider.ExpectAndReturn("GetUnitPrice", 99, 1); this.m_dataAccessProvider.ExpectAndReturn("GetProductName", "The Moon", 1); this.m_dataAccessProvider.ExpectAndReturn("GetUnitPrice", 47, 5); this.m_dataAccessProvider.ExpectAndReturn("GetProductName", "Love", 5);
Console.WriteLine("TestExpectAndReturnOfUnitPriceAndProductName: {0}", (this.m_dataAccessProvider == null));
Basket b = new Basket((IShoppingDataAccess)this.m_dataAccessProvider.MockInstance); b.AddItem(new BasketItem(1, 2, (IShoppingDataAccess)this.m_dataAccessProvider.MockInstance)); b.AddItem(new BasketItem(5, 1, (IShoppingDataAccess)this.m_dataAccessProvider.MockInstance));
b.Save(); Decimal subTotal = b.CalculateSubTotal(); Assert.That(245, Is.EqualTo(subTotal));
this.m_dataAccessProvider.Verify(); }
[Test] public void TestExplicitExpectAndReturnOfUnitPriceAndProductName() { Basket basket = null; BasketItem item1 = null; BasketItem item2 = null; List<BasketItem> itemsCol = null;
this.m_dataAccessProvider.Strict = true;
this.m_dataAccessProvider.ExpectAndReturn("GetUnitPrice", 99, 1); this.m_dataAccessProvider.ExpectAndReturn("GetProductName", "The Moon", 1); this.m_dataAccessProvider.ExpectAndReturn("GetUnitPrice", 47, 5); this.m_dataAccessProvider.ExpectAndReturn("GetProductName", "Love", 5);
Console.WriteLine("TestExplicitExpectAndReturnOfUnitPriceAndProductName: {0}", (this.m_dataAccessProvider == null));
item1 = new BasketItem(1, 2, (IShoppingDataAccess)this.m_dataAccessProvider.MockInstance); item2 = new BasketItem(5, 1, (IShoppingDataAccess)this.m_dataAccessProvider.MockInstance); itemsCol = new List<BasketItem>() { item1, item2 };
this.m_dataAccessProvider.Expect("SaveBasketItems", Is.TypeOf(typeof(Guid)), itemsCol);
basket = new Basket((IShoppingDataAccess)this.m_dataAccessProvider.MockInstance); basket.AddItem(item1); basket.AddItem(item2);
basket.Save();
Decimal subTotal = basket.CalculateSubTotal(); Assert.That(245, Is.EqualTo(subTotal));
this.m_dataAccessProvider.Verify(); }
[Test] public void TestConfigExpectAndReturnOfUnitPriceAndProductName() { Basket basket = null; BasketItem item1 = null; BasketItem item2 = null; List<BasketItem> itemsCol = null;
this.m_dataAccessProvider.Strict = true;
this.m_dataAccessProvider.ExpectAndReturn("GetUnitPrice", 99, 1); this.m_dataAccessProvider.ExpectAndReturn("GetProductName", "The Moon", 1); this.m_dataAccessProvider.ExpectAndReturn("GetUnitPrice", 47, 5); this.m_dataAccessProvider.ExpectAndReturn("GetProductName", "Love", 5);
Console.WriteLine("TestExplicitExpectAndReturnOfUnitPriceAndProductName: {0}", (this.m_dataAccessProvider == null));
item1 = new BasketItem(1, 2, (IShoppingDataAccess)this.m_dataAccessProvider.MockInstance); item2 = new BasketItem(5, 1, (IShoppingDataAccess)this.m_dataAccessProvider.MockInstance); itemsCol = new List<BasketItem>() { item1, item2 };
this.m_dataAccessProvider.Expect("SaveBasketItems", Is.TypeOf(typeof(Guid)), itemsCol);
basket = new Basket((IShoppingDataAccess)this.m_dataAccessProvider.MockInstance); basket.AddItem(item1); basket.AddItem(item2); basket.Save();
Decimal subTotal = basket.CalculateSubTotal(); Assert.That(245, Is.EqualTo(subTotal));
this.m_dataAccessProvider.Verify(); }
[TearDown] public void TestTearDown() { this.m_dataAccessProvider = null; Console.WriteLine("TestTearDown: {0}", (this.m_dataAccessProvider == null)); }
[TestFixtureTearDown] public void FixtureTearDown() { Console.WriteLine("FixtureTearDown"); } }

A dynamic mock object is created, which mimics method calls to the DataAccessProvider class. The actual DataAccessProvider class is not invoked here, but rather a mock instance containing the methods defined in the IShoppingDataAccess interface.

Four tests are defined in this Test class, with appropriate Setup and TearDown methods at both test and class levels. Debug messages are written to the console for illustration purposes only.