Sunday 14 October 2018

Create a Unit Test Case in C# using Moq

In this post we are going to see how to create a unit test case in C# using Moq. First we create a library project and write the following code.

public interface ICalculator
    {
       (string operation, int result) DoOperation(string operation, int a, int b);
    }


    public class Calculator:ICalculator
    {
        public (string operation,int result) DoOperation(string operation,int a,int b)
        {
            (string operation, int result) operationResult;
            int c = 0;
            switch (operation)
            {
                case "add":
                    c = a + b;
                    break;
                case "sub":
                    c = a - b;
                    break;
                case "mul":
                    c = a * b;
                    break;
                default:
                    c = a + b;
                    break;
            }
            operationResult =(operation, c);
            return operationResult;
        }
    }

public class CalMachine
    {
        private ICalculator calc;

        public CalMachine():this(new Calculator())
        {

        }

        public CalMachine(ICalculator obj)
        {
            this.calc = obj;
        }

       public (string operation, int result) Operate(string operationType, int a , int b)
        {
            return calc.DoOperation(operationType, a, b);
        }

    }


Now create a unit test case project and add the reference of library project to the unit test case project
Then install the Moq using nuget package manager. Mock the interface and pass that object to the instance. Then setup the method DoOperation, where the method mock and returns the response which has mentioned in the Returns

[TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            // Arrange
            Moq.Mock<ICalculator> cal = new Moq.Mock<ICalculator>();
         cal.Setup(x => x.DoOperation(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))                                        .Returns(("add", 3));
            CalMachine machine = new CalMachine(cal.Object);

            // ACT
            (string operation, int result) result = machine.Operate("add", 1, 3);

            // Assert
            Assert.AreEqual("add", result.operation);
            Assert.AreEqual(3, result.result);
        }
    }



From this code you can learn how to write a unit test case for c# project using Moq. 

2 comments:

  1. We at COEPD glad to announce that we have introduced Dot Net Technologies Internship Programs (Self sponsored) for professionals who want to have hands on experience. This program is available in COEPD Hyderabad premises which is accompanied by IT Companies. It is intelligently dedicated to our firm participants predominantly acknowledging and appreciating the fact that they are on the path of making a career in Dot Net Technologies discipline. We assume Object-Oriented Programming concepts and teaches C#.NET, ADO.NET which helps the interns to build database-driven Web applications and Web Sites successfully. This internship is designed to gain theoretical knowledge and also hands-on practice and practical know-how to master the nitty-gritty of the Dot Net developer profession. More than a training institute, COEPD today stands differentiated as a mission to help you "Build your dream career" - COEPD way.

    https://www.coepd.com/DotNet-Internship.aspx

    ReplyDelete