Wednesday, February 22, 2017

Fundamentals of Unit Testing: Unit Testing of IOC Code

Original: http://www.c-sharpcorner.com/UploadFile/dacca2/fundamentals-of-unit-testing-unit-testing-of-ioc-code/

This is the Fundamentals of Unit Testing article series. In this article we are discussing various important concepts of unit testing. You can read them here.
This article explains the advantages of IoC implementation in code. I hope we all have a basic concept of IoC and now we will learn how unit testing can be performed in IoC implemented code and then we will see to use a mock object in the same code.

Here is the code snippet that we will test. It's a small message sending operation and the implementation is like the following. 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Classes
{
    public interface ISendr
    {
        string send();
    }

    public class smsSender : ISendr
    {
        public string send()
        {
            return "SMS send";
        }
    }

    public class emailSender : ISendr
    {
        public String send()
        {
            return "Email send";
        }
    }

    public class voiceSend : ISendr
    {
        public string send()
        {
            return "VOICE Send";
        }
    }
    public class sender
    {
        public string send(ISendr obj)
        {
            return obj.send();
        }
    }
}

Ok, so now we will write the unit test for this unit of code. Have a look at the following example.

using System;
using Classes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace UNIT
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            Assert.AreEqual("SMS send"new sender().send(new smsSender()));
            Assert.AreEqual("Email send"new sender().send(new emailSender()));
            Assert.AreEqual("VOICE Send"new sender().send(new voiceSend()));
        }
    }
}

Since we have implemented all our classes like sendSMS, sendMAIL then we can send a concrete object of the implemented class as the argument of the send() function implemented in the IoC class. And if we run the test, we will see that all the tests passed, here is the output.



Now, let's assume that we did not implemente the ISender interface in any class. Then how will we create the mock object?

To create a mock object we will use the MOQ framework. So provide a reference of the MOQ framework in your project through the Nuget Package Manager.

Now, if we think that we did not implement the ISender interface in any class, then we can create one mock object of ISender and we can pass it as an argument of the send() function defined in the sender() method. Here is a sample implementation.

using System;
using Classes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace UNIT
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var mock = new Mock<ISendr>();
            var objsender = new sender().send(mock.Object);
        }
    }
}

If we run the unit test application, we will see that the test passed.



Conclusion

In this article we saw how to implement unit testing of IoC implemented code. I hope it provides a basic understanding of unit testing when an application is implemented using IoC.