如何使用 NSubstitute 检查收到的对索引器的调用

How to check received calls to indexer with NSubstitute

鉴于此界面:

public interface ITest
{
  bool this[string parameter] { get; }
}

如何检查收到的索引器调用?

ITest test = Substitute.For<ITest>();
test.Received()["value"];  // Won't compile

编译错误为:

Error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement (CS0201)

要解决此问题,您可以进行虚拟分配:

ITest test = Substitute.For<ITest>();
var ignored = test.Received()["value"];