1
0
Files
common/test/AMWD.Common.Tests/Extensions/ExceptionExtensionsTest.cs

74 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
namespace AMWD.Common.Tests.Extensions
{
[TestClass]
public class ExceptionExtensionsTest
{
[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<Exception>
{
new("Inner Exception 1."),
new("Inner Exception 2. See the inner exception for details.", new Exception("Inner Exception of Exception 2.")),
new("Inner Exception 3."),
new("Inner Exception 4."),
new("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);
}
}
}