如何使用nsubstitute覆盖set属性方法

时间:2013-10-03 02:24:09

标签: c# wpf unit-testing mvvm nsubstitute

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

作为一种解决方法,我想使用nsubstitute拦截此属性上的set方法,当参数为null时,请忽略它,或将其设置为其他内容。

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

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

这里的FYI是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()  

0 个答案:

没有答案