public ExpectedExceptionCommand(TestCommand innerCommand, Type expectedType, string expectedMessage, MessageMatch matchType) : base(innerCommand) { _expectedType = expectedType; _expectedMessage = expectedMessage; _matchType = matchType; }
/// <summary> /// Convenient utility mehtod used as an alternative to the /// <see cref="ExpectedExceptionAttribute"/>. This a method allows /// to test multiple exception in one test case. /// </summary> /// <remarks> /// <example> /// Example code to assert two similar exceptions. /// <code language="c#"> /// TestHelper.AssertException<ArgumentNullException>( /// delegate { service.GetDepartmentCodes(null); }); /// /// TestHelper.AssertException<ArgumentNullException>( /// delegate { service.GetCategoryCodes(null, 0, 0); } /// MessageMatch.Contains, "subjectCode"); /// </code> /// </example> /// </remarks> /// <typeparam name="T">type of exception</typeparam> /// <param name="call">The code to be executed that throws exception</param> /// <param name="match">To specify how the message is matched</param> /// <param name="message">The message to match. <see langword="null"/> /// or empty string will not cause assertion.</param> public static Exception AssertException <T>(ThreadStart call, MessageMatch match, string message) where T : Exception { try { call(); } catch (T e) { if (!String.IsNullOrEmpty(message)) { switch (match) { case MessageMatch.Regex: Regex regex = new Regex(message); if (!regex.IsMatch(e.Message)) { Assert.Fail(string.Format("Expected regex {0} does not match actual exception message {1}.", message, e.Message)); } break; case MessageMatch.Contains: if (!e.Message.Contains(message)) { Assert.Fail(string.Format("Expected string '{0}' is not found in actual exception message: '{1}'.", message, e.Message)); } break; default: Assert.AreEqual(message, e.Message, "Exception "); break; } } return(e); } Assert.Fail("Expecting " + typeof(T).FullName + ", but no exception was throw."); return(null); }
public ExpectedExceptionAttribute(string exceptionName, string expectedMessage) : this(exceptionName) { this.expectedMessage = expectedMessage; this.matchType = MessageMatch.Exact; }
public ExpectedExceptionAttribute(string exceptionName, string expectedMessage) : this(exceptionName) { this.expectedMessage = expectedMessage; this.matchType = MessageMatch.Exact; }