Thursday 7 November 2013

Stub VS Shim VS Mock. Differences between Microsoft.Fake framework and Moq framework.

using System;
using Microsoft.QualityTools.Testing.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MicrosoftFakesFrameworkPlayClassLibrary;
using Moq;

namespace UnitTestProject
{
    [TestClass]
    public class UnitTest2
    {
        [TestMethod]
        public void TestUsingStub()
        {
            IStockFeed stockFeed =
                new MicrosoftFakesFrameworkPlayClassLibrary.Fakes.StubIStockFeed()
                {
                    GetSharePriceString = (company) => 7890
                };

            var actual = stockFeed.GetSharePrice("bbc");
        }

        [TestMethod]
        public void TestUsingShim()
        {
            using (ShimsContext.Create())
            {
                System.Fakes.ShimDateTime.NowGet = () => new DateTime(2000, 1, 1);

                var now = DateTime.Now;

                Assert.AreEqual(2000, now.Year);
            }
        }

        // To use Mock, you need to deal with Mock Wrap object. While as MS Fakes framework already generated 
        // Stub and Shim objects for you.
        [TestMethod]
        public void TestUsingMock()
        {
            var mock = new Mock();
            mock.Setup(instance => instance.GetSharePrice(It.IsAny())).Returns(1234);

            //Action
            var actual = mock.Object.GetSharePrice("myCompany");

            //Assert
            Assert.AreEqual(1234, actual);
        }
    }
}
                                                                                                                       

Saturday 2 February 2013

Maybe class for null handling for reference types in C#

"Tony Hoare, the inventor of the null reference, called it his ‘billion dollar mistake.’ Null is a disaster because it means that every reference type has a magic value that will destroy your program. For value types, we now have ‘normal’ value types and nullable value types to express the difference between, say, ‘this function returns an integer’ and ‘this function might return an integer, or it might not.’ But we don’t have that for reference types."
For example, in C# we have int for a normal value and int? for a nullable value. But for reference type like a string, you just don't know until you check it or your program crashed on it.

Ivan Towlson has mentioned a good and compact way to handle nullable type with a Maybe class. Since I don't have his source code, I created my own. If you are interested to use it, just simply download the Maybe.cs class under the MaybeApplication folder from github and use it in your own program. Or you can download the whole project. The project is created in VS2010.

Why should you care? What does the class do?

Ok, instead of doing the null check the old fashion way
        static string DemoTheOldWayOfNullChecking()
        {
            Contact contact = new Contact();
            contact.Name = "Sean";
            contact.Phone = "12345678";
            contact.Age = 24;
            //contact.PersonalPet = new Pet()
            //{
            //    PetName = new PetName()
            //        {
            //            Name = "My Pet"
            //        }
            //};

            if (contact == null)
                return null;

            if (contact.PersonalPet == null)
                return null;

            if (contact.PersonalPet.PetName == null)
                return null;

            var petName = contact.PersonalPet.PetName.Name;
            if (petName == null)
                return null;

            return petName;
        }

Now with some Linq awesomeness, you can do something like this
        static string DemoTheNewWayOfNullChecking()
        {
            Contact contact = new Contact();
            contact.Name = "Sean";
            contact.Phone = "12345678";
            contact.Age = 24;
            //contact.PersonalPet = new Pet()
            //{
            //    PetName = new PetName()
            //        {
            //            Name = "My Pet"
            //        }
            //};

            // Maybe class allow you to do something similar to int? for the reference types.
            Maybe maybeContact = new Maybe(contact);

            var maybePetName = from c in maybeContact
                          from cp in c.PersonalPet
                          from pn in cp.PetName
                          select pn.Name;

            if (maybePetName.HasValue)
                return maybePetName.Value;

            return null;
        }


Reference:
http://www.mindscapehq.com/blog/index.php/2012/03/27/5-12-f-features-every-c-programmer-should-lust-after/