加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – MOQ – 验证异常被抛出

发布时间:2020-12-15 03:52:43 所属栏目:百科 来源:网络整理
导读:我正在使用最小起订量框架进行测试. 我有一个场景,我期望抛出一个故障异常. 如何验证它是否被抛出? public void Koko(Liststring? list) { foreach(string? str in list) { if (str != null) someProperty.Foo(str); else throw new FormatException(); } }
我正在使用最小起订量框架进行测试.
我有一个场景,我期望抛出一个故障异常.
如何验证它是否被抛出?
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读