Answer the question
In order to leave comments, you need to log in
How to handle exceptions in xUnit.net?
Can you please tell me how to handle the thrown exception in the xUnit.net unit testing framework?
In my case, if one of the properties is less than zero, then an exception will be thrown. For this module, I wrote tests:
If the numbers are positive, then everything is fine, the tests are successfully executed, if at least one of the members is negative, then the test ends with an error, and an exception is reported in the description.
How can I describe in the test that this is exactly the exception I want, and that the given test should pass in this case?
Answer the question
In order to leave comments, you need to log in
[Fact]
public void Test2()
{
Assert.Throws<ShapeException>(() =>
{
var rect = new Rectangle
{
Height = -5,
Width = 3
};
rect.Area = rect.Height * rect.Width;
}
);
}
Add a new condition, else if
If you catch exceptions then try catch
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question