Mocking classes that implement IQueryable with Moq

后端 未结 5 1126
隐瞒了意图╮
隐瞒了意图╮ 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: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 that derives from System.Linq.EnumerableQuery:

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

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

    var items = new Item[0];
    
    var repo = new Mock(items).As();
    

提交回复
热议问题