using System; using System.Collections.Generic; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace AMWD.Common.Tests.Extensions { [TestClass] [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] public class ExceptionExtensionsTests { [TestMethod] public void ShouldReturnExceptionMessage() { // arrange var exception = new Exception("This is a message."); // act string message = exception.GetMessage(); // assert Assert.AreEqual(exception.Message, message); } [TestMethod] public void ShouldReturnInnerExceptionMessage() { // arrange var innerException = new Exception("Message from the inner side."); var outerException = new Exception("Message from the outer side.", innerException); // act string message = outerException.GetMessage(); // assert Assert.AreEqual(innerException.Message, message); } [TestMethod] public void ShouldReturnRecursiveExceptionMessageFoInnerException() { // arrange var innerException = new Exception("Message from the inner side."); var outerException = new Exception("Message from the outer side. See the inner exception for details.", innerException); string expectedMessage = $"Message from the outer side. Message from the inner side."; // act string message = outerException.GetRecursiveMessage(); // assert Assert.AreEqual(expectedMessage, message); } [TestMethod] public void ShouldReturnRecursiveExceptionMessageFoInnerExceptions() { // arrange var innerExceptions = new List { new Exception("Inner Exception 1."), new Exception("Inner Exception 2. See the inner exception for details.", new Exception("Inner Exception of Exception 2.")), new Exception("Inner Exception 3."), new Exception("Inner Exception 4."), new Exception("Inner Exception 5.") }; var aggregateException = new AggregateException("Lots of exceptions.", innerExceptions); string expectedMessage = "Inner Exception 1. Inner Exception 2. Inner Exception of Exception 2. Inner Exception 3."; // act string message = aggregateException.GetRecursiveMessage(); // assert Assert.AreEqual(expectedMessage, message); } } }