Moqing Entity Framework 6 .Include() using DbSet<>

前端 未结 5 967
南笙
南笙 2020-12-07 17:47

I\'d like to give the background to this question. Skip if you like. For quite a while I\'ve paid close attention to the on going debates on stackoverflow and elsewhere rega

5条回答
  •  孤城傲影
    2020-12-07 18:14

    For anyone who stumbles upon this issue with interest on how to solve the .Include("Foo") problem with NSubstitute and Entity Framework 6+, I was able to bypass my Include calls in the following way:

    var data = new List()
    {
        /* Stub data */
    }.AsQueryable();
    
    var mockSet = Substitute.For, IQueryable>();
    ((IQueryable)mockSet).Provider.Returns(data.Provider);
    ((IQueryable)mockSet).Expression.Returns(data.Expression);
    ((IQueryable)mockSet).ElementType.Returns(data.ElementType);
    ((IQueryable)mockSet).GetEnumerator().Returns(data.GetEnumerator());
    
    // The following line bypasses the Include call.
    mockSet.Include(Arg.Any()).Returns(mockSet);
    

提交回复
热议问题