I am trying to unit test an async method below using VSTest. However, the test passes for AsyncMathsStatic.Divide(4, 0) as well as AsyncMathsStatic.Divide(4, 1)) even though an exception is thrown in the first case only.
[TestClass]
public class UnitTest1
{
[TestMethod]
public void DivideTest1()
{
// Func<Task> action = async()=> { AsyncMathsStatic.Divide(4, 0); };
//action.Should().Throw<DivideByZeroException>();
Assert.ThrowsExceptionAsync<DivideByZeroException>(async () =>
AsyncMathsStatic.Divide(4, 0));
}
}
public class AsyncMathsStatic
{
public static async void Divide(int v1, int v2)
{
try
{
if (v1/v2 > 1)
{
// do something time consuming
}
}
catch (DivideByZeroException ex)
{
throw;
}
}
}
async voidcan get you into a lot of trouble.