简体   繁体   English

如何在 Xunit 中使用 Assert.Throws 引发异常

[英]how to throw an exception using Assert.Throws in Xunit

i am trying to write a test case using Xunit where i want check if the text i am passing is not expected one throw exception saying the value should be the same only我正在尝试使用 Xunit 编写一个测试用例,我想检查我传递的文本是否不是预期的一个抛出异常,说值应该是相同的

Here is my code这是我的代码

   [Theory]
   [InlineData("Goods","Goods")]
   [InlineData("Test","Goods")]
   public void Vehicle(string use,string expected)
   {
      // Arrange
      var risk= CreateRisk();
      var request = new Request();

      risk.Use = use;
      
      // Act
      Test().Mapping(risk, request);
      
      // Assert
     Assert.Throws<ArgumentException>(expected != "Goods" ? "Vehicle Use Should be with Goods": expected);
  
   }

I am not sure how i can frame this.我不知道我怎么能框架这个。 Thanks in advance提前致谢

You need to capture the exception result during your act:您需要在执行过程中捕获异常结果:

  // Act
  var result = Assert.Throws<ArgumentException(() => Test().Mapping(risk, request));
  
  // Assert
 result.Message.Should().Be(expected);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM