Saturday, November 16, 2013

TDD using Moq

Recently I was experimenting TDD in C# using Moq mocking framework. An awesome mocking framework. Some samples are below;

For more details on Moq refer this link;
https://code.google.com/p/moq/

[TestMethod]
        public void TestEnrollCustomer()
        {
            var enrollment = new Mock(MockBehavior.Strict, MockCustomerDetails.One);
            bool actual = false;
              actual=  enrollment.Object.EnrollCustomer();
            Assert.AreEqual(true, actual, "Failed to execute");
        }

public class MockCustomerDetails
    {
        static CustomerDetails customer1 = null;
        static CustomerDetails customer2 = null;
        static CustomerDetails customer3 = null;
        static MockCustomerDetails()
        {
            customer1 = new CustomerDetails(MockCustomer.One, MockBIB.One, MockLockBox.One);
            customer2 = new CustomerDetails(MockCustomer.Two, MockBIB.Two, MockLockBox.Two);
            customer3 = new CustomerDetails(MockCustomer.Three, MockBIB.Three, MockLockBox.Three);
        }

        public static CustomerDetails One
        {
            get
            {
                return customer1;
            }
        }
        public static CustomerDetails Two
        {
            get
            {
                return customer2;
            }
        }
        public static CustomerDetails Three
        {
            get
            {
                return customer3;
            }
        }
    }

public class MockBIB
    {
        static DummyBIB bib1 = null;
        static DummyBIB bib2 = null;
        static DummyBIB bib3 = null;

        static MockBIB()
        {
            bib1 = new DummyBIB("1213121");
            bib2 = new DummyBIB("12131zcxc21");
            bib3 = new DummyBIB("12wewe13121");
        }
        public static DummyBIB One
        {
            get
            {
                return bib1;
            }
        }
        public static DummyBIB Two
        {
            get
            {
                return bib2;
            }
        }
        public static DummyBIB Three
        {
            get
            {
                return bib3;
            }
        }
    }

public class MockLockBox
    {
        static DummyLockBox lock1 = null;
        static DummyLockBox lock2 = null;
        static DummyLockBox lock3 = null;

        static MockLockBox()
        {
            lock1 = new DummyLockBox("1213121");
            lock2 = new DummyLockBox("121sdsad3121"); 
            lock3 = new DummyLockBox("1213wrtwrtrw121");
        }
        public static DummyLockBox One
        {
            get
            {
                return lock1;
            }
        }
        public static DummyLockBox Two
        {
            get
            {
                return lock2;
            }
        }
        public static DummyLockBox Three
        {
            get
            {
                return lock3;
            }
        }
    }

No comments:

Post a Comment