How do you mock adding items to a repository or DbContext using moq?

前端 未结 5 1125
猫巷女王i
猫巷女王i 2021-01-23 17:00

The examples I\'ve seen for using moq for a repository only show how to mock things being returned. I have a somewhat strange requirement: when a query is executed, if a conditi

5条回答
  •  伪装坚强ぢ
    2021-01-23 17:27

    Times have changed -- since the release of Entity Framework 6 it has become much easier to mock database context and datasets. This article outlines the particulars.

    Testing non-query scenarios

    using Microsoft.VisualStudio.TestTools.UnitTesting; 
    using Moq; 
    using System.Data.Entity; 
    
    namespace TestingDemo 
    { 
        [TestClass] 
        public class NonQueryTests 
        { 
            [TestMethod] 
            public void CreateBlog_saves_a_blog_via_context() 
            { 
                var mockSet = new Mock>(); 
    
                var mockContext = new Mock(); 
                mockContext.Setup(m => m.Blogs).Returns(mockSet.Object); 
    
                var service = new BlogService(mockContext.Object); 
                service.AddBlog("ADO.NET Blog", "http://blogs.msdn.com/adonet"); 
    
                mockSet.Verify(m => m.Add(It.IsAny()), Times.Once()); 
                mockContext.Verify(m => m.SaveChanges(), Times.Once()); 
            } 
        } 
    }
    

    Testing query scenarios Query testing is pretty sweet now, because you can build up test data sets in code and then execute your tests against them:

    using Microsoft.VisualStudio.TestTools.UnitTesting; 
    using Moq; 
    using System.Collections.Generic; 
    using System.Data.Entity; 
    using System.Linq; 
    
    namespace TestingDemo 
    { 
        [TestClass] 
        public class QueryTests 
        { 
            [TestMethod] 
            public void GetAllBlogs_orders_by_name() 
            { 
                var data = new List 
                { 
                    new Blog { Name = "BBB" }, 
                    new Blog { Name = "ZZZ" }, 
                    new Blog { Name = "AAA" }, 
                }.AsQueryable(); 
    
                var mockSet = new Mock>(); 
                mockSet.As>().Setup(m => m.Provider).Returns(data.Provider); 
                mockSet.As>().Setup(m => m.Expression).Returns(data.Expression); 
                mockSet.As>().Setup(m => m.ElementType).Returns(data.ElementType); 
                mockSet.As>().Setup(m => m.GetEnumerator()).Returns(0 => data.GetEnumerator()); 
    
                var mockContext = new Mock(); 
                mockContext.Setup(c => c.Blogs).Returns(mockSet.Object); 
    
                var service = new BlogService(mockContext.Object); 
                var blogs = service.GetAllBlogs(); 
    
                Assert.AreEqual(3, blogs.Count); 
                Assert.AreEqual("AAA", blogs[0].Name); 
                Assert.AreEqual("BBB", blogs[1].Name); 
                Assert.AreEqual("ZZZ", blogs[2].Name); 
            } 
        } 
    }
    

提交回复
热议问题