NSubstitute DbSet / IQueryable

后端 未结 5 1452
心在旅途
心在旅途 2020-12-02 12:12

So EntityFramework 6 is a lot better testable then previous versions. And there are some nice examples on the internet for frameworks like Moq, but the case is, I prefer usi

5条回答
  •  臣服心动
    2020-12-02 13:05

    You shouldn't need to mock all of the pieces of the IQueryable. When I use NSubstitute for mocking an EF DbContext I do something like so:

    interface IContext
    {
      IDbSet Foos { get; set; }
    }
    
    var context = Substitute.For();
    
    context.Foos.Returns(new MockDbSet());
    

    With a simple implementation of IDbSet around a list or something for my MockDbSet().

    In general you should be mocking interfaces, not types as NSubstitute will only override virtual methods.

提交回复
热议问题