0

I've derived a small test from Microsoft article on testing EF with moq: Testing with a mocking framework

namespace TestingDemo
{
    public class BloggingContext : DbContext
    {
        public virtual DbSet<Blog> Blogs { get; set; }
    }

    public class Blog
    {
        public string Name { get; set; }
    }

    [TestClass]
    public class QueryTests
    {
        [TestMethod]
        public void Test()
        {
            var data = new List<Blog> { new Blog { Name = "AAA" } }.AsQueryable();
            var mockSet = new Mock<DbSet<Blog>>();
            //var a = mockSet.Object;
            mockSet.As<IQueryable<Blog>>().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As<IQueryable<Blog>>().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As<IQueryable<Blog>>().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As<IQueryable<Blog>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
            var b = mockSet.Object.ToList();
        }
    }
}

It works fine until I uncomment the line var a = mockSet.Object; After this I end up with exception on the line var b = mockSet.Object.ToList();

System.NotImplementedException: 'The member 'IEnumerable.GetEnumerator' has not been implemented on type 'DbSet`1Proxy' which inherits from 'DbSet`1'. Test doubles for 'DbSet`1' must provide implementations of methods and properties that are used.'

Am i missing something?

UPD: Using Moq 4.10.1, EF 6.2.0, VS Community 2019 16.6.1, .NET 4.7.2

2
  • I would suggest you stick to what was shown in the linked documentation. There is no need to be accessing the mocked object that early in the setup anyway
    – Nkosi
    Jun 4, 2020 at 10:38
  • Hm... this is very likely because the mock create object using lazy initialization, when you invoke Object, so once it is created it want be created again. Initialization takes care of the DynamicProxy creation so any additional interface you add after won't be "mocked"...
    – Johnny
    Jun 4, 2020 at 11:57

2 Answers 2

0

You actually need to create DbContext mock as well like:

var context = new Mock<BloggingDbContext>();

var mockSet = new Mock<DbSet<Blog>>();

 mockSet.As<IQueryable<Blog>>().Setup(m => m.Provider).Returns(data.Provider);
 mockSet.As<IQueryable<Blog>>().Setup(m => m.Expression).Returns(data.Expression);
 mockSet.As<IQueryable<Blog>>().Setup(m => m.ElementType).Returns(data.ElementType);
 mockSet.As<IQueryable<Blog>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

context.Setup(c => c.Blogs).Returns(mockSet.Object);

var blogs = context.Object.Blogs.ToList(); // usage 
5
  • Are you able to reproduce the original error? What happens if you introduce var a = mockSet.Object; before doing the setups like in the provided example
    – Nkosi
    Jun 4, 2020 at 10:34
  • @Nkosi no this is what we used to do in our .net core app and it works fine Jun 4, 2020 at 10:35
  • My thoughts exactly. I am wondering if the problem is reproducible since it should work from what is currently shown in the OP
    – Nkosi
    Jun 4, 2020 at 10:36
  • Definitely, but I need to understand why var a = mockSet.Object; before calling moq.As<>().Setup() changes the behavior. Calling moq.Setup() works fine.
    – ayartsev
    Jun 4, 2020 at 11:14
  • You do not need to create a db context instance/mock to mock a db set.
    – rgvlee
    Jun 4, 2020 at 12:28
0

The problem is you are adding the IQueryable<T> setups after you have obtained an instance of the mocked object (mockSet.Object); it won't work and that's also why it works fine once you comment out the assignment to a. You should only retrieve the mocked object once you have added setups for the behaviour you want to consume.

Interestingly once you have added those setups you can change them (override using another setup) at any time. You've just got to do the initial registration prior to retrieving the mocked object. I do it all the time with my system mock libraries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.