NSubstitute out数组,参数不明确?

时间:2018-05-07 06:19:28

标签: c# .net nsubstitute

NSubstitute抱怨我的论点含糊不清,但据我所知,他们完全没有规范。我添加了更多详细信息,但我已经将其归结为这个小例子了。 (编辑:现在更小,删除了参数,但没有参数的定义。)

我的最终目标是'内联方法'作为测试的辅助方法,输入结果,以及表示错误数组的预期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(

2 个答案:

答案 0 :(得分:2)

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

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

在这种情况下,三个参数匹配器排队,但fakeTest .Received().testMethod(bool a, bool b)仅占用两个,因此NSubstitute不确定三个参数匹配器的位置。

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

答案 1 :(得分:0)

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

离开已接收的电话中的Arg.Is,或者不接听电话。接收或评论孤儿论证似乎可以解决它,但我不知道为什么。