如何使用 NSubstitute 模拟 DbContext,然后使用 add/remove 数据

How do I mock DbContext using NSubstitute and then add/remove data

我需要模拟 EF 的 DbContext。我使用 the approach here 并且效果很好。

// 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 DbContext
var mockContext = Substitute.For<MyDbContextClass>();
mockContext.Set<Foo>().Returns(mockDbSet);

但是在某些测试中,我需要能够调用 mockContext.Set<Foo>().Add(someFoo)mockContext.Set<Foo>().Remove(otherFoo),并且底层的 add/remove 逻辑才能工作。

我试过这个:

mockDbSet.When(x => x.Add(Arg.Any<Foo>())).Do(x => data.Add(x.Arg<Foo>()));

但它抛出 Collection was modified; enumeration operation may not execute.

那么如何实现 add/remove 功能?

您不想将其添加到 collection。您想要做的是检查它 (add/remove/etc) 是否被调用以及可能是用什么调用的。

// arrange - check what it was called with. You place asserts in the body of the `Do` expression. Place this before your call that actually executes the code
mockDbSet.Add(Arg.Do<Foo>(foo =>
{
    Assert.IsNotNull(foo);
    // Assert other properties of foo
}));

// act


// assert. Make sure that it was actually called
mockDbSet.Received(1).Add(Arg.Any<Foo>());

如果您想在稍后的测试中添加 Foo,您可以保留对 FooList 的引用。

// mock a DbSet
var mockDbSet = Substitute.For<DbSet<Foo>, IQueryable<Foo>>();
var fooList = new List<Foo>();
var data = fooList.AsQueryable();
// rest of your code unchanged

// add it from the code being tested through the mock dbset
mockDbSet.Add(Arg.Do<Foo>(foo =>
{
    fooList.Add(foo);
    // at this point you have to recreate the added IQueryable
    data = fooList.AsQueryable();
    // rest of code you had to add this to the mockDbSet
}));


// act

@Stilgar 的评论让我研究了 EntityFramework.Testing library, which solves this very problem

所以我用这个库替换了我的东西(少了一件需要担心的事情)。