Unit Testing – Moq

Moq – it is library to mock your objects, return fake values. It is helpful when unit testing.
Imagine that you have Employee class and HrManager which performs any logic on Employees and you want to test this logic easily. Moq can be helpful in such situations.

See our code to be tested.

        public interface IEmployee
            int SkillsLevel { get; }
            int ExperienceLevel { get; }
        class Employee : IEmployee
            public Employee(string lastName, int experience, int skills)
                LastName = lastName;
                ExperienceLevel = experience;
                SkillsLevel = skills;
            public string LastName { get; set; }

            public int ExperienceLevel { get; set; }
            public int SkillsLevel { get; set; }

        class HrManager
            public static IEmployee GetTheBestEmployee(IList<IEmployee> employees)
                if (employees == null || employees.Count == 0)
                    throw new ArgumentNullException();

                return employees.ToList().OrderByDescending(e => e.SkillsLevel + e.ExperienceLevel).FirstOrDefault();

We would like to test HrManager.GetTheBestEmployee. We need a few instances of IEmployee to do this. Below you can see how mock can help in this situation. Notice that Employees are not instantiated in normal way. Mock allows you to setup only the required properties.

        public void MoqUnitTest1()
            var mockedEmployees = new List<IEmployee>();

            var e1 = new Moq.Mock<IEmployee>(); //create mocked IEmployee
            e1.Setup(x => x.SkillsLevel).Returns(5); //sets that SkillsLevel property will return value = 5, when called
            e1.Setup(x => x.ExperienceLevel).Returns(2); //sets that ExperienceLevel property will return value = 2, when called
            mockedEmployees.Add(e1.Object); //object here is IEmployee

            var e2 = new Moq.Mock<IEmployee>();
            e2.Setup(x => x.SkillsLevel).Returns(10);
            e2.Setup(x => x.ExperienceLevel).Returns(10);

            var e3 = new Moq.Mock<IEmployee>();
            e3.Setup(x => x.SkillsLevel).Returns(2);
            e3.Setup(x => x.ExperienceLevel).Returns(1);

            var theBestOne = HrManager.GetTheBestEmployee(mockedEmployees); //invoked method to be tested
            Assert.AreEqual<int>(10 * 10, theBestOne.SkillsLevel * theBestOne.ExperienceLevel); //assert
            e3.Verify(x => x.ExperienceLevel, Times.Once); //verifies whether property ExperienceLevel was called exactly once for this object

More information: https://github.com/Moq/moq4/wiki/Quickstart


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s