且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NSubstitute DbSet / IQueryable的< T>

更新时间:2022-02-10 14:50:32

这是因为特定的NSubstitute语法。例如:

This happens because of NSubstitute syntax specific. For example in:

((IQueryable<Blog>) mockSet).Provider.Returns(data.Provider);

NSubstitute调用提供的getter,那么它指定的返回值。此getter通话不被截获的替代品,你会得到一个异常。这是因为明确实施的DBQuery类IQueryable.Provider财产。

NSubstitute calls the Provider's getter, then it specifies the return value. This getter call isn't intercepted by the substitute and you get an exception. It happens because of explicit implementation of IQueryable.Provider property in DbQuery class.

您可以明确地创建用于NSUB多个接口的替代品,并创建涵盖所有指定接口的代理。然后调用接口将被替代被截获。
请使用以下语法:

You can explicitly create substitutes for multiple interfaces with NSub, and it creates a proxy which covers all specified interfaces. Then calls to the interfaces will be intercepted by the substitute. Please use the following syntax:

// Create a substitute for DbSet and IQueryable types:
var mockSet = Substitute.For<DbSet<Blog>, IQueryable<Blog>>();

// And then as you do:
((IQueryable<Blog>)mockSet).Provider.Returns(data.Provider);
...