ExpectedException Assert

I need to write unit test for the next function, and I saw that I can use [ExpectedException]

This is a proven feature.

public static T FailIfEnumIsNotDefined<T>(this T enumValue, string message = null) where T:struct { var enumType = typeof (T); if (!enumType.IsEnum) { throw new ArgumentOutOfRangeException(string.Format("Type {0} is not an Enum, therefore it cannot be checked if it is Defined not have defined.", enumType.FullName)); } else if (!Enum.IsDefined(enumType, enumValue)) { throw new ArgumentOutOfRangeException(string.Format("{1} Value {0} is not does not have defined value in Enum of type {0}. It should not be...", enumType.FullName, message ?? "")); } return enumValue; } 

and here will go code to check for exceptions that should be thrown

  [TestMethod] [ExpectedException(ArgumentOutOfRangeException(ArgumentException), "message")] public void FailIfEnumIsNotDefined_Check_That_The_Value_Is_Not_Enum() { // PREPARE // EXECUTE // ASSERT } 

I have no idea to make a statement for exceptions.

+6
source share
6 answers

ExpectedException simply states that an exception of the specified type will be thrown by the test method:

 [TestMethod] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void FailIfEnumIsNotDefined_Check_That_The_Value_Is_Not_Enum() { // PREPARE // EXECUTE // NO ASSERT!! } 

If you want to assert other exception parameters, you should use try..catch in your test method:

 [TestMethod] public void FailIfEnumIsNotDefined_Check_That_The_Value_Is_Not_Enum() { // PREPARE try { // EXECUTE Assert.Fail() } catch(Exception exception) { // ASSERT EXCEPTION DETAILS } } 

You can write your own exception exception method to avoid repeating the same test code again and again:

 public TException AssertCatch<TException>(Action action) where TException : Exception { try { action(); } catch (TException exception) { return exception; } throw new AssertFailedException("Expected exception of type " + typeof(TException) + " was not thrown"); } 

Using:

 var exception = AssertCatch<ArgumentOutOfRangeException>(() => /* EXECUTE */); Assert.AreEqual("foo", exception.Message); 
+17
source

You should use ExpectedException otherwise:

 [TestMethod] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void MyTestSomething() 

and then program your test to get the expected exception.

+2
source

You probably want to find some additional exception handling options. MSTest ExpectedException attr has several drawbacks. Try using this approach, which gives you excellent control over an abandoned exception.

+1
source

To approve the exception is to throw with the correct exception message using:

 var ex = Assert.Throws<Exception>(() => _foo.DoSomething(a, b, c)); Assert.That(ex.Message, Is.EqualTo("Your exception message")); 
+1
source

While an ExpectedException cannot be used as-to to check an exception message, you can implement your own exception-checking logic by inheriting from ExpectedExceptionBaseAttribute :

Introducing your own expected exception check. you can specify additional information and requirements that the built-in methods of the ExpectedExceptionAttribute class cannot be processed, for example, the following:

  • Check exception status.
  • Waiting for more than one type of exception.
  • Displays a custom message when an incorrect exception type occurs.
  • Negative test result control.

In your case, it might look something like this:

 public sealed class ExpectedExceptionMessageAttribute<T> : ExpectedExceptionBaseAttribute { readonly string _expectedMessage; public ExpectedExceptionMessageAttribute(string expectedMessage) { _expectedMessage = expectedMessage; } protected override void Verify(System.Exception exception) { // Handle assertion exceptions from assertion failures in the test method base.RethrowIfAssertException(exception); Assert.IsInstanceOfType(exception, typeof(T), "wrong exception type"); Assert.AreEqual(_expectedMessage, exception.Message, "wrong exception message"); } } 

HAving said that I would still be inclined to use the direct try - catch approach, although, more specifically, where exactly is the exception exception expected:

 public static void Throws<T>(Action action, Predicate<T> predicate = null) where T : Exception { try { action(); } catch (T e) { if (predicate == null || predicate(e)) { return; } Assert.Fail($"Exception of type {typeof(T)} thrown as expected, but the provided predicate rejected it: {e}"); } catch (Exception e) { Assert.Fail($"Expected exception of type {typeof(T)} but a different exception was thrown: {e}"); } Assert.Fail($"No exception thrown, expected {typeof(T)}"); } 
+1
source

You do not need an assertion, if you use the ExpectedException attribute, in fact your code cannot arrive at an assertion.

appearance: http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.expectedexceptionattribute.aspx

If you want to make sure that an exception is thrown, you must put Assert.Fail () after the operation that should throw an exception, in this case, if no exception is thrown, the test will fail.

0
source

All Articles