How to check that a method throws an exception in MSTest?
Hello everyone! I'm new to unit testing in C# and using MSTest. I have a method that should throw an exception under certain conditions. For example, here's a simple division method:
```csharp
public int Divide(int a, int b)
{
if (b == 0)
throw new DivideByZeroException("Cannot divide by zero!");
return a / b;
}
```
I want to write a test that checks that when passing b = 0, a DivideByZeroException is actually thrown. But I can't figure out how to do this correctly using Assert. I tried wrapping the call in try-catch and putting Assert.Fail, but it looks very messy. I saw that in other frameworks (e.g., NUnit) there is a special [ExpectedException] attribute, but in MSTest it seems to be deprecated or doesn't work the same way. Please advise
No answers yet.
Log in to leave an answer