Site Loader
Auckland, New Zealand
In the last post we discussed about the introduction of Moq and its usage by both developers and testers. We also discussed how we can empower Moq for testing application without using UI. In this post we will discuss writing a simple code with and without using Moq.

Code without Moq

In this code we have used the concrete class rather its abstract interface
//Arrange
var pfDetails = new EmpPfDetails(new EmpPersonalDetails());

//Act
var contrib = pfDetails.GetPfEmployerControlSofar(1);

//Assert
Assert.That(contrib,Is.EqualTo(3455),"Its not as expected");

Here is the complete video of the above code

Code with Moq

Using Moq, you can see that we have replaced the dependency of concrete class EmpPersonalDetails with Moq object of IEmpPersonalDetails and passed that object to EmpPfDetails
  	    //Arrange
            var moqPersonalDetails = new Mock();
            var pfDetails = new EmpPfDetails(moqPersonalDetails.Object);

            //Act
            pfDetails.GetPfEmployerControlSofar(It.IsAny());

            //Assert
            moqPersonalDetails.Verify();
Here is the complete video of the above code Thanks for reading the post and watching the video!!! Please leave your comments and let me know if there is anything I should update in this post. Thanks, Karthik KK

Post Author: Karthik kk

Leave a Reply

Your email address will not be published. Required fields are marked *