微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NSubstitute 测试仅在构建服务器中失败

如何解决NSubstitute 测试仅在构建服务器中失败

在 c# 单元测试项目中使用 NSubstite 框架进行模拟,最近我们创建了 version2 API 控制器和业务和存储库。但是当为控制器创建测试时,第一个案例在 Azure 构建管道中失败。相同的测试用例运行没有错误,状态为通过。我已经尝试了博客中的所有方法,但没有运气。这是错误代码

错误

Test method XXX threw exception:
NSubstitute.Exceptions.RedundantArgumentMatcherException: Some argument specifications (e.g. Arg.Is,Arg.Any) were left over after the last call.

This is often caused by using an argument spec with a call to a member NSubstitute does not handle (such as a non-virtual member or a call to an instance which is not a substitute),or for a purpose other than specifying a call (such as using an arg spec as a return value). For example:

var sub = Substitute.For<SomeClass>();
var realType = new MyRealType(sub);
// INCORRECT,arg spec used on realType,not a substitute:
realType.someMethod(Arg.Any<int>()).Returns(2);
// INCORRECT,arg spec used as a return value,not to specify a call:
sub.VirtualMethod(2).Returns(Arg.Any<int>());
// INCORRECT,arg spec used with a non-virtual method:
sub.NonVirtualMethod(Arg.Any<int>()).Returns(2);
// CORRECT,arg spec used to specify virtual call on a substitute:
sub.VirtualMethod(Arg.Any<int>()).Returns(2);
To fix this make sure you only use argument specifications with calls to substitutes. If your substitute is a class,make sure the member is virtual.

Another possible cause is that the argument spec type does not match the actual argument type,but code compiles due to an implicit cast. For example,Arg.Any<int>() was used,but Arg.Any<double>() was required.

NOTE: the cause of this exception can be in a prevIoUsly executed test. Use the diagnostics below to see the types of any redundant arg specs,then work out where they are being created.

Diagnostic information:

Remaining (non-bound) argument specifications:
any Int64
any Int64

All argument specifications:
any Int64
any Int64
any Int64
any Int64


Stack trace
at NSubstitute.Core.Arguments.ArgumentSpecificationsFactory.Create(IList`1 argumentSpecs,Object[] arguments,IParameterInfo[] parameterInfos,MethodInfo methodInfo,MatchArgs matchArgs)
at NSubstitute.Core.CallSpecificationFactory.CreateFrom(ICall call,MatchArgs matchArgs)
at NSubstitute.Routing.Handlers.RecordCallSpecificationHandler.Handle(ICall call)
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 NSubstitute.Proxies.CastleDynamicProxy.ProxyIdInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.ObjectProxy_4.GetTemplateByApplicationIdTemplateId(Int64 applicationId,Int64 templateId)
at XXX() in YYY.cs:line 61

public Interface XXX
{
long Process(long x,string y,out string z);
}

[TestMethod]
public void XXX()
{
XXX _business = Substitute.For<XXX>();
Object model = GetFakeModel();
_business.Process(Arg.Any<long>(),Arg.Any<string>(),out Arg.Any<string>())
.Returns(x => { x[2] = "Success"; return 1; });
var _controller = new YYY(_business);
var result = _controller.Post(1,model) as ObjectResult;
Assert.IsNotNull(result);
}

我们如何解决这个问题。即使我无法调试它在本地机器上的传递。尝试通过添加注释 [DoNotParallelize] 按顺序运行测试,但没有成功。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。