c# – nsubstitute收到调用特定对象参数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – nsubstitute收到调用特定对象参数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1369字,纯文字阅读大概需要2分钟。
内容图文
![c# – nsubstitute收到调用特定对象参数](/upload/InfoBanner/zyjiaocheng/809/57502148fd094b969110e86cdc79810d.jpg)
我有一个看起来像这样的类:
public myArguments
{
public List<string> argNames {get; set;}
}
在我的测试中,我这样做:
var expectedArgNames = new List<string>();
expectedArgNames.Add("test");
_mockedClass.CheckArgs(Arg.Any<myArguments>()).Returns(1);
_realClass.CheckArgs();
_mockedClass.Received().CheckArgs(Arg.Is<myArguments>(x => x.argNames.Equals(expectedArgNames));
但测试失败并显示以下错误消息:
NSubstitute.Exceptions.ReceivedCallsException : Expected to receive a call matching:
CheckArgs(myArguments)
Actually received no matching calls.
Received 1 non-matching call (non-matching arguments indicated with '*' characters):
CheckArgs(*myArguments*)
我猜这是因为.Equals(),但我不知道如何解决它?
解决方法:
在您的测试中,您将myArguments类与List< string>进行比较.
您应该将myArguments.argNames与List< string>进行比较.或实现IEquatable< List< string>>在myArguments中.
此外,当您比较List< T>时,您应该使用SequenceEqualsinstead Equals.
第一种选择是:
_mockedClass.Received().CheckArgs(
Arg.Is<myArguments>(x => x.argNames.SequenceEqual(expectedArgNames)));
第二个是:
public class myArguments : IEquatable<List<string>>
{
public List<string> argNames { get; set; }
public bool Equals(List<string> other)
{
if (object.ReferenceEquals(argNames, other))
return true;
if (object.ReferenceEquals(argNames, null) || object.ReferenceEquals(other, null))
return false;
return argNames.SequenceEqual(other);
}
}
内容总结
以上是互联网集市为您收集整理的c# – nsubstitute收到调用特定对象参数全部内容,希望文章能够帮你解决c# – nsubstitute收到调用特定对象参数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。