Created
January 14, 2013 14:35
-
-
Save mhinze/4530469 to your computer and use it in GitHub Desktop.
Extend the ridiculous MSTest ExpectedExceptionBaseAttribute to assert on message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.RegularExpressions; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace $NAMESPACE$ | |
{ | |
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] | |
public sealed class ExpectedExceptionWithMessageAttribute : ExpectedExceptionBaseAttribute | |
{ | |
readonly bool _allowDerivedTypes; | |
readonly Type _exceptionType; | |
readonly string _expectedMessagePattern; | |
public ExpectedExceptionWithMessageAttribute(Type exceptionType, | |
string expectedMessage, | |
bool allowDerivedExceptionTypes) | |
: this(exceptionType, expectedMessage, string.Empty, allowDerivedExceptionTypes) { } | |
public ExpectedExceptionWithMessageAttribute(Type exceptionType, string expectedMessage) | |
: this(exceptionType, expectedMessage, string.Empty, false) { } | |
public ExpectedExceptionWithMessageAttribute(Type exceptionType, | |
string expectedMessage, | |
string noExceptionMessage, | |
bool allowDerivedTypes) | |
: base(noExceptionMessage) | |
{ | |
if (exceptionType == null) | |
throw new ArgumentNullException("exceptionType", "Parameter \"exceptionType\" may not be null."); | |
if (!typeof (Exception).IsAssignableFrom(exceptionType)) | |
throw new ArgumentException( | |
"The expected exception type must be System.Exception or a type derived from System.Exception.", | |
"exceptionType"); | |
if (expectedMessage == null) | |
throw new ArgumentNullException("expectedMessage", "Parameter \"expectedMessage\" may not be null."); | |
_exceptionType = exceptionType; | |
_expectedMessagePattern = expectedMessage; | |
_allowDerivedTypes = allowDerivedTypes; | |
} | |
protected override void Verify(Exception exception) | |
{ | |
var exceptionType = exception.GetType(); | |
if (_allowDerivedTypes) | |
{ | |
if (!_exceptionType.IsAssignableFrom(exceptionType)) | |
{ | |
RethrowIfAssertException(exception); | |
HandleInvalidExpectedExceptionOrMessage( | |
"Test method {0}.{1} threw exception {2}, but exception {3} or a type derived from it was expected.\r\nException message: {4}.", | |
exception); | |
} | |
else if (!string.Equals(exception.Message, _expectedMessagePattern, StringComparison.Ordinal)) | |
{ | |
HandleInvalidExpectedExceptionOrMessage( | |
"Test method {0}.{1} threw expected exception {2} with message \"{4}\" but message with pattern \"{5}\" was expected.", | |
exception); | |
} | |
} | |
else | |
{ | |
if (exceptionType != _exceptionType) | |
{ | |
RethrowIfAssertException(exception); | |
HandleInvalidExpectedExceptionOrMessage( | |
"Test method {0}.{1} threw exception {2}, but exception {3} was expected.\r\nException message: {4}.", | |
exception); | |
} | |
else if (!string.Equals(exception.Message, _expectedMessagePattern, StringComparison.Ordinal)) | |
{ | |
HandleInvalidExpectedExceptionOrMessage( | |
"Test method {0}.{1} threw expected exception {2} with message \"{4}\" but message with pattern \"{5}\" was expected.", | |
exception); | |
} | |
} | |
} | |
void HandleInvalidExpectedExceptionOrMessage(string messageTemplate, Exception exceptionThrow) | |
{ | |
Assert.Fail(messageTemplate, | |
TestContext.FullyQualifiedTestClassName, | |
TestContext.TestName, | |
exceptionThrow.GetType().FullName, | |
_exceptionType.FullName, | |
exceptionThrow.Message, | |
_expectedMessagePattern); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment