
我有一个场景,我期望抛出一个故障异常.
如何验证它是否被抛出?
public voID Koko(List<string?> List) { foreach(string? str in List) { if (str != null) someProperty.Foo(str); else throw new FormatException(); } } 提前致谢.
解决方法 如果您想要验证异常(由您自己的代码),那么Moq不是您的首选工具.只需使用可用的单元测试框架之一.
的xUnit / NUnit的
Assert.Throws<SomeException>(() => foo.bar());
流利的断言
Action act = () => foo.bar());act.ShouldThrow<SomeException>()
http://fluentassertions.codeplex.com/documentation
http://www.nunit.org/index.php?p=exceptionAsserts&r=2.6.2
总结以上是内存溢出为你收集整理的c# – MOQ – 验证异常被抛出全部内容,希望文章能够帮你解决c# – MOQ – 验证异常被抛出所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)