ExpectedException xunit .net核心

时间:2016-12-23 13:24:57

标签: unit-testing asp.net-core .net-core xunit xunit.net

我正在为核心应用程序编写单元测试。我试图检查,我的班级抛出异常。但 ExpectedException 属性会抛出编译异常:

  

错误CS0246类型或命名空间名称' ExpectedException'不能   找到(你错过了使用指令或程序集   参考?)EventMessagesBroker.Logic.UnitTests..NETCoreApp,Version = v1.0

我的代码:

[Fact]
[ExpectedException(typeof(MessageTypeParserException))]
public void TestMethod1_Error_twoMathces()
{
    var message = "some text";
    var parser = new MessageTypeParser();
    var type = parser.GetType(message);
    Assert.Equal(MessageType.RaschetStavkiZaNalichnye, type);
}

那么,有没有正确的方法来实现呢?

1 个答案:

答案 0 :(得分:38)

在需要例外的代码上使用Assert.Throws

[Fact]
public void TestMethod1_Error_twoMathces()
{
    var message = "some text";
    var parser = new MessageTypeParser();
    Assert.Throws<MessageTypeParserException>(() => parser.GetType(message));
}