Click here to Skip to main content
15,861,168 members
Articles / Visual Studio

Mock a Database Repository using Moq

Rate me:
Please Sign up or sign in to vote.
4.85/5 (18 votes)
15 Dec 2009CPOL2 min read 164.8K   35   17
How to mock a database repository using Moq

The concept of unit testing my code is still fairly new to me and was introduced when I started writing applications with the Microsoft MVC Framework in Visual Studio 2008. Intimidated somewhat by the Moq library's heavy reliance on lambdas, my early tests used full Mock classes that I would write myself, and which implemented the same interface as my real database repositories. I'd only write the code for the methods I needed, all other methods would simply throw a "NotImplementedException". However, I quickly discovered that the problem with this approach is that whenever a new method was added to the interface, my test project would no longer build (since the new method was not implemented in my mock repository) and I would have to manually add a new method that threw another "NotImplementedException". After doing this for the 5th or 6th time, I decided to face my fears and get to grips with using the Moq library instead. Here is a simple example, of how you can mock a database repository class using the Moq library.

Let's assume that your database contains a table called Product, and that either you or Linq, or LLBLGen, or something similar has created the following class to represent that table as an object in your class library.

The Product Class

C#
namespace MoqRepositorySample
{
    using System;

    public class Product
    {
        public int ProductId { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public double Price { get; set; }

        public DateTime DateCreated { get; set; }

        public DateTime DateModified { get; set; }
    }
}

Your Product Repository class might implement an interface similar to the following, which offers basic database functionality such as retrieving a product by id, by name, fetching all products, and a save method that would handle inserting and updating products.

The IProductRepository Interface

C#
namespace MoqRepositorySample
{
    using System.Collections.Generic;
 
    public interface IProductRepository
    {
        IList<Product> FindAll();
 
        Product FindByName(string productName);
 
        Product FindById(int productId);
 
        bool Save(Product target);
    }
}

The test class that follows demonstrates how to use Moq to set up a mock Products repository based on the interface above. The unit tests shown here focus primarily on testing the mock repository itself, rather than on testing how your application uses the repository, as they would in the real world.

Microsoft Unit Test Class

C#
namespace TestProject1
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
 
    using Moq;

    using MoqRepositorySample;

    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public UnitTest1()
        {
            // create some mock products to play with
            IList<Product> products = new List<Product>
                {
                    new Product { ProductId = 1, Name = "C# Unleashed",
                        Description = "Short description here", Price = 49.99 },
                    new Product { ProductId = 2, Name = "ASP.Net Unleashed",
                        Description = "Short description here", Price = 59.99 },
                    new Product { ProductId = 3, Name = "Silverlight Unleashed",
                        Description = "Short description here", Price = 29.99 }
                };
 
            // Mock the Products Repository using Moq
            Mock<IProductRepository> mockProductRepository = new Mock<IProductRepository>();

            // Return all the products
            mockProductRepository.Setup(mr => mr.FindAll()).Returns(products);

            // return a product by Id
            mockProductRepository.Setup(mr => mr.FindById(
                It.IsAny<int>())).Returns((int i) => products.Where(
                x => x.ProductId == i).Single());

            // return a product by Name
            mockProductRepository.Setup(mr => mr.FindByName(
                It.IsAny<string>())).Returns((string s) => products.Where(
                x => x.Name == s).Single());

            // Allows us to test saving a product
            mockProductRepository.Setup(mr => mr.Save(It.IsAny<Product>())).Returns(
                (Product target) =>
                {
                    DateTime now = DateTime.Now;
 
                    if (target.ProductId.Equals(default(int)))
                    {
                        target.DateCreated = now;
                        target.DateModified = now;
                        target.ProductId = products.Count() + 1;
                        products.Add(target);
                    }
                    else
                    {
                        var original = products.Where(
                            q => q.ProductId == target.ProductId).Single();
 
                        if (original == null)
                        {
                            return false;
                        }
 
                        original.Name = target.Name;
                        original.Price = target.Price;
                        original.Description = target.Description;
                        original.DateModified = now;
                    }
 
                    return true;
                });
 
            // Complete the setup of our Mock Product Repository
            this.MockProductsRepository = mockProductRepository.Object;
        }
 
        /// <summary>
        /// Gets or sets the test context which provides
        /// information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }
 
        /// <summary>
        /// Our Mock Products Repository for use in testing
        /// </summary>
        public readonly IProductRepository MockProductsRepository;
 
        /// <summary>
        /// Can we return a product By Id?
        /// </summary>
        [TestMethod]
        public void CanReturnProductById()
        {
            // Try finding a product by id
            Product testProduct = this.MockProductsRepository.FindById(2);
 
            Assert.IsNotNull(testProduct); // Test if null
            Assert.IsInstanceOfType(testProduct, typeof(Product)); // Test type
            Assert.AreEqual("ASP.Net Unleashed", testProduct.Name); // Verify it is the right product
        }
 
        /// <summary>
        /// Can we return a product By Name?
        /// </summary>
        [TestMethod]
        public void CanReturnProductByName()
        {
            // Try finding a product by Name
            Product testProduct = this.MockProductsRepository.FindByName("Silverlight Unleashed");
 
            Assert.IsNotNull(testProduct); // Test if null
            Assert.IsInstanceOfType(testProduct, typeof(Product)); // Test type
            Assert.AreEqual(3, testProduct.ProductId); // Verify it is the right product
        }
 
        /// <summary>
        /// Can we return all products?
        /// </summary>
        [TestMethod]
        public void CanReturnAllProducts()
        {
            // Try finding all products
            IList<Product> testProducts = this.MockProductsRepository.FindAll();
 
            Assert.IsNotNull(testProducts); // Test if null
            Assert.AreEqual(3, testProducts.Count); // Verify the correct Number
        }
 
        /// <summary>
        /// Can we insert a new product?
        /// </summary>
        [TestMethod]
        public void CanInsertProduct()
        {
            // Create a new product, not I do not supply an id
            Product newProduct = new Product
                { Name = "Pro C#", Description = "Short description here", Price = 39.99 };
 
            int productCount = this.MockProductsRepository.FindAll().Count;
            Assert.AreEqual(3, productCount); // Verify the expected Number pre-insert
 
            // try saving our new product
            this.MockProductsRepository.Save(newProduct);
 
            // demand a recount
            productCount = this.MockProductsRepository.FindAll().Count;
            Assert.AreEqual(4, productCount); // Verify the expected Number post-insert
 
            // verify that our new product has been saved
            Product testProduct = this.MockProductsRepository.FindByName("Pro C#");
            Assert.IsNotNull(testProduct); // Test if null
            Assert.IsInstanceOfType(testProduct, typeof(Product)); // Test type
            Assert.AreEqual(4, testProduct.ProductId); // Verify it has the expected productid
        }
 
        /// <summary>
        /// Can we update a product?
        /// </summary>
        [TestMethod]
        public void CanUpdateProduct()
        {
            // Find a product by id
            Product testProduct = this.MockProductsRepository.FindById(1);
 
            // Change one of its properties
            testProduct.Name = "C# 3.5 Unleashed";
 
            // Save our changes.
            this.MockProductsRepository.Save(testProduct);
 
            // Verify the change
            Assert.AreEqual("C# 3.5 Unleashed", this.MockProductsRepository.FindById(1).Name);
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Salem Web Network
United States United States
Robert Williams has been programming web sites since 1996 and employed as .NET developer since its release in 2002.

Comments and Discussions

 
QuestionExcellent work Pin
Godfrey Shongwe6-May-20 1:52
Godfrey Shongwe6-May-20 1:52 
QuestionGood Article - But Example is not correct Pin
udew.net2-Jan-18 23:04
udew.net2-Jan-18 23:04 
QuestionWonderful ! Pin
KK Kod7-Aug-15 12:17
KK Kod7-Aug-15 12:17 
QuestionHow can I use this test with Entity Framework? Pin
Member 107995614-Feb-15 5:46
Member 107995614-Feb-15 5:46 
QuestionNo Controllers? Pin
Adnan Salahuddin2-Jan-15 0:54
Adnan Salahuddin2-Jan-15 0:54 
GeneralMy vote of 5 Pin
Sturms5-Oct-14 8:24
Sturms5-Oct-14 8:24 
GeneralMy vote of 5 Pin
blackninja720-Mar-13 14:36
blackninja720-Mar-13 14:36 
GeneralFantastic Article Pin
Allen Conway1-Mar-13 10:33
Allen Conway1-Mar-13 10:33 
Questionprice datatype! Pin
Jalal Khordadi5-Apr-12 21:32
Jalal Khordadi5-Apr-12 21:32 
GeneralThat's not a test! Pin
janus00713-Jan-11 9:26
janus00713-Jan-11 9:26 
GeneralRe: That's not a test! Pin
makv200918-Oct-11 0:03
makv200918-Oct-11 0:03 
GeneralRe: That's not a test! Pin
Aurimas21-Mar-12 22:31
Aurimas21-Mar-12 22:31 
GeneralRe: That's not a test! Pin
bnjkukunuri31-Jan-15 4:44
bnjkukunuri31-Jan-15 4:44 
GeneralRe: That's not a test! Pin
Aurimas31-Jan-15 5:01
Aurimas31-Jan-15 5:01 
GeneralRe: That's not a test! Pin
bnjkukunuri31-Jan-15 6:43
bnjkukunuri31-Jan-15 6:43 
GeneralRe: That's not a test! Pin
Aurimas31-Jan-15 8:45
Aurimas31-Jan-15 8:45 
GeneralRhino vs moq Pin
Sohel_Rana18-Dec-09 21:04
Sohel_Rana18-Dec-09 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.