N用不明确的参数替换数组?

NSubstitute out arrays, with ambiguous parameters?

NSubstitute 抱怨我的论点模棱两可,但据我所知,它们是完全规范的。我会添加更多细节,但我已经将其归结为这个小例子。 (编辑: 现在更小,删除了参数,但没有参数的定义。)

我的最终目标是 'inlined method' 成为测试的辅助方法,输入结果,以及代表错误数组的预期 ITextObjC[]。

给出最小的、完整的、可验证的例子:

    public interface test
    {
        bool testMethod(
            bool boolA,
            bool boolB);
    }

    public interface ITestObjC { }

    public class TestObjC : ITestObjC { }

    [Test]
    public void SillyTest2()
    {
        var fakeTest = Substitute.For<test>();

        fakeTest.testMethod(  false, false);

        ITestObjC[] recOutArr = Arg.Is<ITestObjC[]>(x => x == null);

        fakeTest.Received(1).testMethod(
            Arg.Is<bool>(false),
            Arg.Is<bool>(false));
    }

结果:

NSubstitute.Exceptions.AmbiguousArgumentsException : Cannot determine argument specifications to use.
Please use specifications for all arguments of the same type.
   at NSubstitute.Core.Arguments.NonParamsArgumentSpecificationFactory.Create(Object argument, IParameterInfo parameterInfo, ISuppliedArgumentSpecifications suppliedArgumentSpecifications)
   at NSubstitute.Core.Arguments.ArgumentSpecificationFactory.Create(Object argument, IParameterInfo parameterInfo, ISuppliedArgumentSpecifications suppliedArgumentSpecifications)
   at NSubstitute.Core.Arguments.MixedArgumentSpecificationsFactory.<>c__DisplayClass3_0.<Create>b__0(Object argument, Int32 i)
   at System.Linq.Enumerable.<SelectIterator>d__5`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at NSubstitute.Core.Arguments.MixedArgumentSpecificationsFactory.Create(IList`1 argumentSpecs, Object[] arguments, IParameterInfo[] parameterInfos)
   at NSubstitute.Core.Arguments.ArgumentSpecificationsFactory.Create(IList`1 argumentSpecs, Object[] arguments, IParameterInfo[] parameterInfos, MatchArgs matchArgs)
   at NSubstitute.Core.CallSpecificationFactory.CreateFrom(ICall call, MatchArgs matchArgs)
   at NSubstitute.Routing.Handlers.CheckReceivedCallsHandler.Handle(ICall call)
   at NSubstitute.Routing.Route.<>c__DisplayClass8_0.<Handle>b__0(ICallHandler x)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at NSubstitute.Routing.Route.Handle(ICall call)
   at NSubstitute.Core.CallRouter.Route(ICall call)
   at NSubstitute.Proxies.CastleDynamicProxy.CastleForwardingInterceptor.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.testProxy.testMethod(ITestObjA motionEnvelope, ITestObjB motionSeries, Boolean primaryLimits, Boolean testPitch, ITestObjC[]& exceedences)
   at HeliSAFE.DataStorage.Tests.SholMonitor.CommonSholMonitorTests.SillyTest2() in D:\Repositories\GitSAFE_Repos\helisafe.container\helisafe\HeliSAFE.DataStorage.Tests\SholMonitor\CommonSholMonitorTests.cs:line 375

这一行:

fakeTest.Received(1).testMethod(

NSubstitute 似乎有某种完整性检查,在评估包含 Arg.Is 的 Received 调用时运行。

在 Received 调用中省略 Arg.Is,或者不调用 .Received,或者注释掉 Orphan 参数似乎可以解决问题,但我不知道为什么。

简短的回答是 don't use argument matchers outside of a Received or Returns

较长的答案是,为了获得 NSubstitute 的语法,它做了一些有问题的事情。在这种情况下,每次您执行 Arg.IsArg.Any 时,它都会将参数匹配器推送到静态(线程本地)队列中。当它收到实际调用时,它会检索这些参数匹配器以计算出匹配的调用(对于 Received)或对存根的调用(对于 Returns)。

在这种情况下,三个参数匹配器排队,但 fakeTest .Received().testMethod(bool a, bool b) 只需要两个,因此 NSubstitute 不确定三个参数匹配器的去向。

顺便说一句,这些情况和错误消息的检测将在下一版本的 NSubstitute(3.1 之后)中得到改进。