mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-30 08:33:19 +01:00
Beware those that read into this commit. Here there be style conformance... Closes #1252
39 lines
1.3 KiB
C#
39 lines
1.3 KiB
C#
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
using Tgstation.Server.Api.Models;
|
|
using Tgstation.Server.Client;
|
|
|
|
namespace Tgstation.Server.Tests
|
|
{
|
|
/// <summary>
|
|
/// Extension methods for the <see cref="Assert"/> <see langword="ckass"/>.
|
|
/// </summary>
|
|
static class ApiAssert
|
|
{
|
|
/// <summary>
|
|
/// Test a given <typeparamref name="TApiException"/> is thrown when a given <paramref name="action"/> is called.
|
|
/// </summary>
|
|
/// <typeparam name="TApiException">The type of the expected <see cref="ApiException"/>.</typeparam>
|
|
/// <param name="action">A <see cref="Func{TResult}"/> resulting in a <see cref="Task"/>.</param>
|
|
/// <param name="expectedErrorCode">The expected <see cref="ApiException.ErrorCode"/>.</param>
|
|
/// <returns>A <see cref="Task"/> representing the running operation,</returns>
|
|
public static async Task ThrowsException<TApiException>(Func<Task> action, ErrorCode? expectedErrorCode)
|
|
where TApiException : ApiException
|
|
{
|
|
try
|
|
{
|
|
await action().ConfigureAwait(false);
|
|
}
|
|
catch (TApiException ex)
|
|
{
|
|
Assert.AreEqual(expectedErrorCode, ex.ErrorCode, $"Wrong error code for expected API exception! Additional Data: {ex.AdditionalServerData}");
|
|
return;
|
|
}
|
|
|
|
Assert.Fail($"Expected exception {typeof(TApiException)}!");
|
|
}
|
|
}
|
|
}
|