-2

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

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

shreyas35
  • 145
  • 9
  • 2
    Why do you want to throw an exception? What is wrong with a simple `Assert.Equal(expected, "whatever")`? – DavidG Jun 30 '22 at 11:22
  • @DavidG actully the file from which i am mapping the data to my class contains text other than "Goods" but i expect only "Goods" and if any text other than this then want to throw exception – shreyas35 Jun 30 '22 at 11:27
  • `Assert.Throws` asserts that the method passed as an argument throws the specified exception. Use `Assert.Equal` to ensure a result matches the expected value. Use `Assert.All` to ensure all items in a collection match other assertions, eg `Assert.All(items,it=>Assert.Equal("Goods",it.Name))` – Panagiotis Kanavos Jun 30 '22 at 11:27

1 Answers1

4

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);
Neil
  • 11,059
  • 3
  • 31
  • 56