MSTest exceptions
1 minute read
MSTest for exception thrown
If you switch between the two frameworks often and use MS test you may find this useful.
Old MSTest
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
In old .NET Framework test library you don’t have the Assert.Throws.
So use below try catch return.
[TestMethod]
public void GetStuff_ThrowsException()
{
string foobarString = "testing stuff";
decimal foobarDecimal = 21;
try
{
var result = _myController.GetStuff(foobarString, foobarDecimal);
}
catch (Exception ex)
{
return;
}
Assert.Fail("Should have thrown an exception.");
}
New MSTest
C:\Users\peter\.nuget\packages\mstest.testframework\1.3.2\lib\netstandard1.0\Microsoft.VisualStudio.TestPlatform.TestFramework.dll
In new .NET Core test library you can use the following methods.
[TestMethod]
public Task GetStuff_ThrowsException_Async()
{
string foobarString = "testing stuff";
decimal foobarDecimal = 21;
await Assert.ThrowsExceptionAsync<Exception>(() => _myController.GetStuffAsync(foobarString, foobarDecimal));
}
[TestMethod]
public void GetStuff_ThrowsException()
{
string foobarString = "testing stuff";
decimal foobarDecimal = 21;
Assert.ThrowsException<Exception>(() => _myController.GetStuff(foobarString, foobarDecimal));
}
I feedback.
Let me know what you think of this article on twitter @M3PGS or leave a comment below!
Let me know what you think of this article on twitter @M3PGS or leave a comment below!
comments powered by Disqus