NUnit using NMock Code Review

Define the Form 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 TestFormShopping { private Mockery m_mockery = null; private IShoppingDataAccess m_mockDataAccessProvider;
[TestFixtureSetUp] public void FixtureInitialise() { Console.WriteLine("FixtureInitialise"); }
[SetUp] public void TestInitialise() { this.m_mockery = new Mockery(); this.m_mockDataAccessProvider = (IShoppingDataAccess)this.m_mockery.NewMock(typeof(IShoppingDataAccess)); Console.WriteLine("TestInitialise: {0}", (this.m_mockery == null)); }
[Test] public void TestSetReturnValueOfUnitPriceAndProductName() { Expect.Once.On(this.m_mockDataAccessProvider).Method("GetUnitPrice").With(1).Will(Return.Value(99)); Expect.Once.On(this.m_mockDataAccessProvider).Method("GetProductName").With(1).Will(Return.Value("The Moon"));
Console.WriteLine("TestSetReturnValueOfUnitPriceAndProductName: {0}", (this.m_mockDataAccessProvider == null));
BasketItem item = new BasketItem(1, 2, this.m_mockDataAccessProvider); Assert.That(99, SyntaxHelpers.Is.EqualTo(item.UnitPrice)); Assert.That("The Moon", SyntaxHelpers.Is.EqualTo(item.ProductName)); Assert.That(198, SyntaxHelpers.Is.EqualTo(item.GetPrice()));
this.m_mockery.VerifyAllExpectationsHaveBeenMet(); }
[Test] public void TestExplicitExpectAndReturnOfUnitPriceAndProductName() { Basket basket = null; BasketItem item1 = null; BasketItem item2 = null; List<BasketItem> itemsCol = null;
Expect.Once.On(this.m_mockDataAccessProvider).Method("GetUnitPrice").With(1).Will(Return.Value(99)); Expect.Once.On(this.m_mockDataAccessProvider).Method("GetProductName").With(1).Will(Return.Value("The Moon")); Expect.Once.On(this.m_mockDataAccessProvider).Method("GetUnitPrice").With(5).Will(Return.Value(47)); Expect.Once.On(this.m_mockDataAccessProvider).Method("GetProductName").With(5).Will(Return.Value("Love"));
Console.WriteLine("TestExplicitExpectAndReturnOfUnitPriceAndProductName: {0}", (this.m_mockDataAccessProvider == null));
item1 = new BasketItem(1, 2, this.m_mockDataAccessProvider); item2 = new BasketItem(5, 1, this.m_mockDataAccessProvider); itemsCol = new List<BasketItem>() { item1, item2 };
Expect.Once.On(this.m_mockDataAccessProvider).Method("SaveBasketItems").With(Is.TypeOf(typeof(Guid)), itemsCol);
basket = new Basket(this.m_mockDataAccessProvider); basket.AddItem(item1); basket.AddItem(item2); basket.Save();
Decimal subTotal = basket.CalculateSubTotal(); Assert.That(245, SyntaxHelpers.Is.EqualTo(subTotal));
this.m_mockery.VerifyAllExpectationsHaveBeenMet(); }
[TearDown] public void TestTearDown() { this.m_mockery = null; this.m_mockDataAccessProvider = null; Console.WriteLine("TestTearDown: {0}", (this.m_mockery == null)); }
[TestFixtureTearDown] public void FixtureTearDown() { Console.WriteLine("FixtureTearDown"); } }

A mockery object is created, which represents a mock object containing the methods defined in the IShoppingDataAccess interface.

Two 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.