Search code examples
.netunit-testingrepositorymoq

Benefits of using Moq and the like


I have ran across some references to using Moq for unit testing. But after looking moq's wiki examples I can't really see anything benefit over just creating a mock repository. Am i missing somethings. below is an example of a mock repository I have created. Can moq optimize this an any way?

    namespace Models
    {
        public class MockImagesRepository : IImagesRepository
        {
            private readonly BlogContext _context;
            private readonly List<ImageViewModel> _imageViewModels;

            public MockImagesRepository()
            {
                _context = new BlogContext();
                _imageViewModels = new List<ImageViewModel>();
                for (var i = 0; i < 10; i++)
                {
                    _imageViewModels.Add(new ImageViewModel
                                             {
                                                 Id = i,
                                                 AlternateText = "AlternateText " + i,
                                                 Description = "Description " + i,
                                                 Name = "Name " + i,
                                             });
                }
                for (var i = 10; i < 20; i++)
                {
                    _imageViewModels.Add(new ImageViewModel
                                             {
                                                 Id = i,
                                                 AlternateText = "AlternateText " + i,
                                                 Description = "Description " + i,
                                                 Name = "Name " + i,
                                             });
                }
                for (var i = 20; i < 30; i++)
                {
                    _imageViewModels.Add(new ImageViewModel
                                             {
                                                 Id = i,
                                                 AlternateText = "AlternateText " + i,
                                                 Description = "Description " + i,
                                                 Name = "Name " + i,
                                             });
                }

            }

            public int GetCount()
            {
                return _imageViewModels.Count;
            }

            public IEnumerable<ImageViewModel> FindRange(string format, int i, int recordsCount)
            {
                return _imageViewModels.Skip(i).Take(recordsCount);
            }

            public void Add(Image image)
            {
            }

            public IEnumerable<Image> GetImages()
            {
                var images = new List<Image>();
                foreach (var imageViewModel in _imageViewModels)
                {
                    images.Add(new Image(imageViewModel));
                }
                return images;
            }

            #region Implementation of IDisposable

            public void Dispose()
            {
                _context.Dispose();
            }

            #endregion


 }
}

Solution

  • Using Moq, you will not need to create your mock objects manually. This alone should save you time.

    Instead of manually creating those mock objects, you will setup Moq to tell it how to behave depending on what/how things are called.

    Using this methodology you will be able to do the following things:

    • Setup various responses based on different inputs or supplying the same results regardless of input (using Setup)

    • Allow you to simplify your testing code by submitting catch all values for parameters (using It.IsAny<> or It.Is<>)

    • Verify the number of times a method has been called, or whether it has been called at all. (using Verify). This will allow you to test that certain dependencies have been called with the correct parameters a defined amount of times.

    • Setup or Verify that the code will throw a certain exception (using Throws)

    When you master Moq, for moderately complex tests cases, you will see a gain in writing your test cases.

    Of course you could do all those things manually, but writing these facilities in your mocks will be time consuming.