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
CodePudding user response:
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);
CodePudding user response:
All Assert
methods throw in the event of a failure. What you should be doing is:
Assert.Equal(expected, "Goods");
If you really. want you can create your own custom method to wrap around Xunit assert and throw your own message:
public static class CustomAssert
{
public static void Equal(string expected, string actual, string failureMessage)
{
try
{
Assert.Equal(expected, actual);
}
catch
{
throw new Exception(failureMessage);
}
}
}