Mocking classes that implement IQueryable with Moq

后端 未结 5 1109
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 08:41

I spent an evening trying to mock an object that implements IQueryable:

public interface IRepo : IQueryable
{
}

The best

相关标签:
5条回答
  • 2020-12-13 09:20

    Rune's answer is awesome and saved me time figuring out how to do the same. Small gotcha is that if you call some IQueryable extension methods on your IQueryable twice (e.g. ToList()) then the second time you'll get no results back. That's because the enumerator is at the end and needs resetting. Using Rhinomocks I changed the implementation for GetEnumerator to:

    mock.Stub(r => r.GetEnumerator()).Do((Func<IEnumerator<T>>) (() => { 
        var enumerator = queryable.GetEnumerator();
        enumerator.Reset();
        return enumerator;
    }));
    

    Hope that saves someone some time.

    0 讨论(0)
  • 2020-12-13 09:28

    I like Rune's answer. Here's a generic IQueryable version:

    public static void SetupIQueryable<TRepository, TEntity>(this Mock<TRepository> mock, IQueryable<TEntity> queryable)
       where TRepository : class, IQueryable<TEntity>
    {
        mock.Setup(r => r.GetEnumerator()).Returns(queryable.GetEnumerator());
        mock.Setup(r => r.Provider).Returns(queryable.Provider);
        mock.Setup(r => r.ElementType).Returns(queryable.ElementType);
        mock.Setup(r => r.Expression).Returns(queryable.Expression);
    }
    
    0 讨论(0)
  • 2020-12-13 09:31

    This is nothing new, just a cleaner way of doing it. I also have repositories where the repository itself is also an IQueryable, so I needed the same thing. I basically just put your code into an extension method like this at the root level of my test project, to make it available to all tests:

    public static class MockExtensions
    {
        public static void SetupIQueryable<T>(this Mock<T> mock, IQueryable queryable)
            where T: class, IQueryable
        {
            mock.Setup(r => r.GetEnumerator()).Returns(queryable.GetEnumerator());
            mock.Setup(r => r.Provider).Returns(queryable.Provider);
            mock.Setup(r => r.ElementType).Returns(queryable.ElementType);
            mock.Setup(r => r.Expression).Returns(queryable.Expression);
        }
    }
    

    This basically just offers reusability, since you're likely to want to do this in several tests, and in each test it makes the intention clear and the mess minimal. :)

    0 讨论(0)
  • 2020-12-13 09:34

    I think that's about the best you can do with Moq. I think a more readable option would be to roll your own FakeRepo<T> that derives from System.Linq.EnumerableQuery<T>:

    public class FakeRepo<T> : EnumerableQuery<T>, IRepo<T>
    {
        public FakeRepo(IEnumerable<T> items) : base(items) { }
    }
    

    Update: You might be able to pull this off by mocking EnumerableQuery<T> then using As<T>():

    var items = new Item[0];
    
    var repo = new Mock<EnumerableQuery<Item>(items).As<IRepo>();
    
    0 讨论(0)
  • 2020-12-13 09:43

    I had the same issue. I've fixed it by changing this line

    mock.Setup(r => r.GetEnumerator()).Returns(queryable.GetEnumerator());
    

    to

    mock.Setup(r => r.GetEnumerator()).Returns(queryable.GetEnumerator);
    

    I hope that additional comments are not required here.

    0 讨论(0)
提交回复
热议问题