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

前端 未结 5 962
南笙
南笙 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:15

    Playing with this and referencing the answers here Setup result for call to extension method it looks like Moq cannot mock static extension methods

    I tried to add:

    mockSet.Setup(t => t.FirstAsync()).Returns(Task.FromResult(data.First()));
    mockSet.Setup(t => t.FirstAsync(It.IsAny>>())).Returns(Task.FromResult(data.First()));
    

    And Moq complains that:

    System.NotSupportedException : Expression references a method that does not belong to the mocked object: t => t.FirstAsync()

    So it seems there are three options:

    1. refactor your code to further isolate dbcontext so you don't have to test this behaviour
    2. switch from DbSet to IDbSet instead of mocking DbContext
    3. allow your tests to create a SQL compact database and populate it with data in order to run your tests

提交回复
热议问题