Skip to content

Instantly share code, notes, and snippets.

@vadim-kovalyov
Created April 1, 2016 19:34
Show Gist options
  • Save vadim-kovalyov/91a14e78e14c7047072b5c22b9f85116 to your computer and use it in GitHub Desktop.
Save vadim-kovalyov/91a14e78e14c7047072b5c22b9f85116 to your computer and use it in GitHub Desktop.
Sometimes in order to test one logical concern you need to use multiple asserts. This class helps with that.
/// <summary>
/// The multi assert.
/// </summary>
/// <remarks>
/// Sometimes in order to test one logical concern you need to use
/// multiple asserts. This class helps with that.
/// </remarks>
public static class MultiAssert
{
/// <summary>
/// The aggregate.
/// </summary>
/// <param name="actions">The actions.</param>
/// <exception cref="Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException">exception</exception>
/// <exception cref="AssertFailedException">exception </exception>
[DebuggerStepThrough]
public static void Aggregate(params Action[] actions)
{
var exceptions = new List<AssertFailedException>();
foreach (Action action in actions)
{
try
{
action();
}
catch (AssertFailedException ex)
{
exceptions.Add(ex);
}
}
IEnumerable<string> assertionTexts =
exceptions.Select(assertFailedException => assertFailedException.Message);
IList<string> assertionTextsList = assertionTexts as IList<string> ?? assertionTexts.ToList();
if (0 != assertionTextsList.Count())
{
throw new AssertFailedException(
assertionTextsList.Aggregate(
(aggregatedMessage, next) => aggregatedMessage + Environment.NewLine + next));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment