using CSExample;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestProject1
{
public sealed class ExpectedArithmeticException : ExpectedExceptionBaseAttribute
{
private string exceptionMessage;
private string wrongExceptionMessage;
public string WrongExceptionMessage
{
get
{
return wrongExceptionMessage;
}
set
{
wrongExceptionMessage = value;
}
}
public ExpectedArithmeticException(string expectedExceptionMessage) : this(expectedExceptionMessage, "No exception was thrown.")
{
}
public ExpectedArithmeticException(string expectedExceptionMessage, string noExceptionMessage)
: base(noExceptionMessage)
{
exceptionMessage = expectedExceptionMessage;
WrongExceptionMessage = "The exception that was thrown does not derive from System.ArithmeticException.";
}
protected override void Verify(System.Exception exception)
{
Assert.IsNotNull(exception);
// Handle assertion exceptions from assertion failures in the test method, since we are not interested in verifying those
base.RethrowIfAssertException(exception);
Assert.IsInstanceOfType(exception, typeof(System.ArithmeticException), wrongExceptionMessage);
Assert.AreEqual(exceptionMessage, exception.Message, "Could not verify the exception message.");
}
}
[TestClass()]
public class DivisionClassTest
{
/* This test will pass because it thows a System.DivideByZeroException which derives from System.ArithmeticException. */
[TestMethod()]
[ExpectedArithmeticException("Attempted to divide by zero.", "An exception was expected, but no exception was thrown.", WrongExceptionMessage = "The wrong type of exception was thrown.")]
public void DivideTest()
{
DivisionClass target = new DivisionClass();
int numerator = 5;
int denominator = 0;
int actual;
actual = target.Divide(numerator, denominator);
}
}
}