A
A
andreyAmaterasu2020-01-27 14:28:57
C++ / C#
andreyAmaterasu, 2020-01-27 14:28:57

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? 5e2ec7c26e485187286939.bin
In my case, if one of the properties is less than zero, then an exception will be thrown. For this module, I wrote tests: 5e2ec8ca24404563371326.bin
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.
5e2ec900aaa61233036088.bin
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

2 answer(s)
A
Aleksandr Salamatov, 2020-01-27
@andreyAmaterasu

[Fact]
        public void Test2()
        {
            Assert.Throws<ShapeException>(() =>
                {
                    var rect = new Rectangle
                    {
                        Height = -5,
                        Width = 3
                    };
                    rect.Area = rect.Height * rect.Width;
                }
            );
        }

D
DanielMcRon, 2020-01-27
@DanielMcRon

Add a new condition, else if
If you catch exceptions then try catch

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question