Unit testing ExpectedException xunit.net内核

Unit testing ExpectedException xunit.net内核,unit-testing,asp.net-core,.net-core,xunit,xunit.net,Unit Testing,Asp.net Core,.net Core,Xunit,Xunit.net,我正在为核心应用程序编写单元测试。我试图检查,我的类抛出异常。但是ExpectedException属性引发编译异常: 错误CS0246无法找到类型或命名空间名称“ExpectedException” 找不到(是否缺少using指令或程序集 参考?)EventMessagesBroker.Logic.UnitTests..NETCoreApp,版本=v1.0 我的代码: [Fact] [ExpectedException(typeof(MessageTypeParserException))]

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

错误CS0246无法找到类型或命名空间名称“ExpectedException” 找不到(是否缺少using指令或程序集 参考?)EventMessagesBroker.Logic.UnitTests..NETCoreApp,版本=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);
}

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

在需要异常的代码上使用
Assert.Throws

[Fact]
public void TestMethod1_Error_twoMathces()
{
    var message = "some text";
    var parser = new MessageTypeParser();
    Assert.Throws<MessageTypeParserException>(() => parser.GetType(message));
}
[事实]
public void TestMethod1_Error_twoMathces()
{
var message=“一些文本”;
var parser=new MessageTypeParser();
抛出(()=>parser.GetType(message));
}

是的,我用这种方式。我认为这是唯一的解决方案只是为了记录,xunit不支持ExpectedException,并且支持答案中显示的方式。正如这里更详细地描述的:如果您想要遵循AAA语法进行单元测试,那么您可以分配parser.GetType(message)我们还可以使用
Record.exception
方法,然后使用
assert.NotNull
方法和
assert.IsType
方法进行验证。请看不要使用xunit而不迟读3年…ExpectedException来自MSTest Nuget…没有任何运气交叉使用该Nuget与xunit