1

在单元测试中,我在使用 nsubstitute 注册它们时替换了所有视图。但是,当我处理 1 个视图时,将 1 个属性(对象类型)设置为 null 时会引发错误。错误是“值不能为空。参数名称:集合”

作为一种解决方法,我想使用 nsubstitute 来拦截此属性的 set 方法,当参数为 null 时,要么忽略它,要么将其设置为其他值。

我尝试了很多变化,我认为 When..Do 是我需要的,但没有运气实现它。这是我的最后一次尝试:

var view = Substitute.For(new[] { registration.Interface }, new object[] { });
view.When(x => x.DataContext = Arg.Is<object>(null)).Do(x => x[0] = new object());

仅供参考,这里是 nsubstitute 错误的堆栈跟踪:

at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)  
at NSubstitute.Core.Arguments.SuppliedArgumentSpecifications..ctor(IDefaultChecker     defaultChecker, IEnumerable`1 argumentSpecifications)  
at NSubstitute.Core.Arguments.SuppliedArgumentSpecificationsFactory.Create(IEnumerable`1 argumentSpecifications)  
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.Core.GetCallSpec.FromCall(ICall call, MatchArgs matchArgs)  
at NSubstitute.Core.ConfigureCall.SetResultForCall(ICall call, IReturn valueToReturn, MatchArgs matchArgs)   
at NSubstitute.Routing.Handlers.PropertySetterHandler.Handle(ICall call)  
at NSubstitute.Routing.Route.<>c__DisplayClass3.<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()  
4

0 回答 0