How do I mock DbContext using NSubstitute and then add/remove data
问题 I need to mock EF's DbContext . I use the approach here and it works well. // mock a DbSet var mockDbSet = Substitute.For<DbSet<Foo>, IQueryable<Foo>>(); var data = new List<Foo>().AsQueryable(); ((IQueryable<Foo>)mockDbSet).Provider.Returns(data.Provider); ((IQueryable<Foo>)mockDbSet).Expression.Returns(data.Expression); ((IQueryable<Foo>)mockDbSet).ElementType.Returns(data.ElementType); ((IQueryable<Foo>)mockDbSet).GetEnumerator().Returns(data.GetEnumerator()); // now add it to a mock