Rename MediatR extensions to GetOrThrow for ISender
Renamed SendOrThrowAsync and SendOrNotFoundAsync extension methods for IMediator to GetOrThrow for ISender, following MediatR best practices. Updated all usages, XML docs, and tests to use ISender and the new method names. Replaced StubMediator with StubSender in tests. Functionality remains the same, but code now aligns with modern MediatR conventions.
This commit is contained in:
@@ -12,13 +12,13 @@ public class MediatorExtensionsTests
|
||||
private sealed class StubRequest<TResponse> : IRequest<TResponse?> { }
|
||||
|
||||
/// <summary>
|
||||
/// Minimal <see cref="IMediator"/> stub that returns a pre-configured response for any <see cref="Send"/> call.
|
||||
/// Minimal <see cref="ISender"/> stub that returns a pre-configured response for any <see cref="ISender.Send{TResponse}"/> call.
|
||||
/// </summary>
|
||||
private sealed class StubMediator : IMediator
|
||||
private sealed class StubSender : ISender
|
||||
{
|
||||
private readonly object? _response;
|
||||
|
||||
public StubMediator(object? response) => _response = response;
|
||||
public StubSender(object? response) => _response = response;
|
||||
|
||||
public Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -43,214 +43,208 @@ public class MediatorExtensionsTests
|
||||
|
||||
public IAsyncEnumerable<object?> CreateStream(object request, CancellationToken cancellationToken = default)
|
||||
=> throw new NotImplementedException();
|
||||
|
||||
public Task Publish(object notification, CancellationToken cancellationToken = default)
|
||||
=> Task.CompletedTask;
|
||||
|
||||
public Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default) where TNotification : INotification
|
||||
=> Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static IMediator CreateMediator<T>(T? response) => new StubMediator(response);
|
||||
private static ISender CreateSender<T>(T? response) => new StubSender(response);
|
||||
|
||||
#endregion
|
||||
|
||||
#region SendOrThrowAsync — non-null scalar
|
||||
#region GetOrThrow<TResponse, TException> — non-null scalar
|
||||
|
||||
[Test]
|
||||
public async Task SendOrThrowAsync_WithNonNullResponse_ReturnsResponse()
|
||||
public async Task GetOrThrow_WithNonNullResponse_ReturnsResponse()
|
||||
{
|
||||
var mediator = CreateMediator<string>("hello");
|
||||
var sender = CreateSender<string>("hello");
|
||||
var request = new StubRequest<string?>();
|
||||
|
||||
var result = await mediator.SendOrThrowAsync(request, () => new InvalidOperationException());
|
||||
var result = await sender.GetOrThrow(request, () => new InvalidOperationException());
|
||||
|
||||
Assert.That(result, Is.EqualTo("hello"));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SendOrThrowAsync — null response
|
||||
#region GetOrThrow<TResponse, TException> — null response
|
||||
|
||||
[Test]
|
||||
public void SendOrThrowAsync_WithNullResponse_ThrowsCustomException()
|
||||
public void GetOrThrow_WithNullResponse_ThrowsCustomException()
|
||||
{
|
||||
var mediator = CreateMediator<string>(null);
|
||||
var sender = CreateSender<string>(null);
|
||||
var request = new StubRequest<string?>();
|
||||
|
||||
var ex = Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => mediator.SendOrThrowAsync(request, () => new InvalidOperationException("custom")));
|
||||
() => sender.GetOrThrow(request, () => new InvalidOperationException("custom")));
|
||||
|
||||
Assert.That(ex!.Message, Is.EqualTo("custom"));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SendOrThrowAsync — empty collection
|
||||
#region GetOrThrow<TResponse, TException> — empty collection
|
||||
|
||||
[Test]
|
||||
public void SendOrThrowAsync_WithEmptyList_ThrowsCustomException()
|
||||
public void GetOrThrow_WithEmptyList_ThrowsCustomException()
|
||||
{
|
||||
var mediator = CreateMediator<List<string>>(new List<string>());
|
||||
var sender = CreateSender<List<string>>(new List<string>());
|
||||
var request = new StubRequest<List<string>?>();
|
||||
|
||||
Assert.ThrowsAsync<ArgumentException>(
|
||||
() => mediator.SendOrThrowAsync(request, () => new ArgumentException("empty")));
|
||||
() => sender.GetOrThrow(request, () => new ArgumentException("empty")));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SendOrThrowAsync — non-empty collection
|
||||
#region GetOrThrow<TResponse, TException> — non-empty collection
|
||||
|
||||
[Test]
|
||||
public async Task SendOrThrowAsync_WithNonEmptyList_ReturnsResponse()
|
||||
public async Task GetOrThrow_WithNonEmptyList_ReturnsResponse()
|
||||
{
|
||||
var expected = new List<int> { 1, 2 };
|
||||
var mediator = CreateMediator<List<int>>(expected);
|
||||
var sender = CreateSender<List<int>>(expected);
|
||||
var request = new StubRequest<List<int>?>();
|
||||
|
||||
var result = await mediator.SendOrThrowAsync(request, () => new InvalidOperationException());
|
||||
var result = await sender.GetOrThrow(request, () => new InvalidOperationException());
|
||||
|
||||
Assert.That(result, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SendOrThrowAsync — string edge case (string implements IEnumerable)
|
||||
#region GetOrThrow<TResponse, TException> — string edge case (string implements IEnumerable)
|
||||
|
||||
[Test]
|
||||
public async Task SendOrThrowAsync_WithEmptyString_ReturnsEmptyString()
|
||||
public async Task GetOrThrow_WithEmptyString_ReturnsEmptyString()
|
||||
{
|
||||
var mediator = CreateMediator<string>("");
|
||||
var sender = CreateSender<string>("");
|
||||
var request = new StubRequest<string?>();
|
||||
|
||||
var result = await mediator.SendOrThrowAsync(request, () => new InvalidOperationException("should not throw"));
|
||||
var result = await sender.GetOrThrow(request, () => new InvalidOperationException("should not throw"));
|
||||
|
||||
Assert.That(result, Is.EqualTo(""));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SendOrNotFoundAsync — non-null scalar
|
||||
#region GetOrThrow<TResponse> (NotFoundException) — non-null scalar
|
||||
|
||||
[Test]
|
||||
public async Task SendOrNotFoundAsync_WithNonNullResponse_ReturnsResponse()
|
||||
public async Task GetOrThrow_NotFound_WithNonNullResponse_ReturnsResponse()
|
||||
{
|
||||
var mediator = CreateMediator<string>("hello");
|
||||
var sender = CreateSender<string>("hello");
|
||||
var request = new StubRequest<string?>();
|
||||
|
||||
var result = await mediator.SendOrNotFoundAsync(request);
|
||||
var result = await sender.GetOrThrow(request);
|
||||
|
||||
Assert.That(result, Is.EqualTo("hello"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SendOrNotFoundAsync_WithExceptionMessage_AndNonNullResponse_ReturnsResponse()
|
||||
public async Task GetOrThrow_NotFound_WithExceptionMessage_AndNonNullResponse_ReturnsResponse()
|
||||
{
|
||||
var mediator = CreateMediator<int>(42);
|
||||
var sender = CreateSender<int>(42);
|
||||
var request = new StubRequest<int?>();
|
||||
|
||||
var result = await mediator.SendOrNotFoundAsync(request, "not found", CancellationToken.None);
|
||||
var result = await sender.GetOrThrow(request, "not found", CancellationToken.None);
|
||||
|
||||
Assert.That(result, Is.EqualTo(42));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SendOrNotFoundAsync — null response
|
||||
#region GetOrThrow<TResponse> (NotFoundException) — null response
|
||||
|
||||
[Test]
|
||||
public void SendOrNotFoundAsync_WithNullResponse_ThrowsNotFoundException()
|
||||
public void GetOrThrow_NotFound_WithNullResponse_ThrowsNotFoundException()
|
||||
{
|
||||
var mediator = CreateMediator<string>(null);
|
||||
var sender = CreateSender<string>(null);
|
||||
var request = new StubRequest<string?>();
|
||||
|
||||
Assert.ThrowsAsync<NotFoundException>(() => mediator.SendOrNotFoundAsync(request));
|
||||
Assert.ThrowsAsync<NotFoundException>(() => sender.GetOrThrow(request));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SendOrNotFoundAsync_WithNullResponse_AndCustomMessage_ThrowsNotFoundExceptionWithMessage()
|
||||
public void GetOrThrow_NotFound_WithNullResponse_AndCustomMessage_ContainsMessage()
|
||||
{
|
||||
const string message = "Entity not found";
|
||||
var mediator = CreateMediator<string>(null);
|
||||
var sender = CreateSender<string>(null);
|
||||
var request = new StubRequest<string?>();
|
||||
|
||||
var ex = Assert.ThrowsAsync<NotFoundException>(
|
||||
() => mediator.SendOrNotFoundAsync(request, message, CancellationToken.None));
|
||||
() => sender.GetOrThrow(request, message, CancellationToken.None));
|
||||
|
||||
Assert.That(ex!.Message, Does.Contain(message));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SendOrNotFoundAsync_WithNullResponse_HasDefaultMessage()
|
||||
public void GetOrThrow_NotFound_WithNullResponse_HasDefaultMessageWithTypeName()
|
||||
{
|
||||
var mediator = CreateMediator<string>(null);
|
||||
var sender = CreateSender<string>(null);
|
||||
var request = new StubRequest<string?>();
|
||||
|
||||
var ex = Assert.ThrowsAsync<NotFoundException>(() => mediator.SendOrNotFoundAsync(request));
|
||||
var ex = Assert.ThrowsAsync<NotFoundException>(() => sender.GetOrThrow(request));
|
||||
|
||||
Assert.That(ex!.Message, Does.Contain(nameof(String)));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SendOrNotFoundAsync — empty collection
|
||||
#region GetOrThrow<TResponse> (NotFoundException) — empty collection
|
||||
|
||||
[Test]
|
||||
public void SendOrNotFoundAsync_WithEmptyList_ThrowsNotFoundException()
|
||||
public void GetOrThrow_NotFound_WithEmptyList_ThrowsNotFoundException()
|
||||
{
|
||||
var mediator = CreateMediator<List<string>>(new List<string>());
|
||||
var sender = CreateSender<List<string>>(new List<string>());
|
||||
var request = new StubRequest<List<string>?>();
|
||||
|
||||
Assert.ThrowsAsync<NotFoundException>(() => mediator.SendOrNotFoundAsync(request));
|
||||
Assert.ThrowsAsync<NotFoundException>(() => sender.GetOrThrow(request));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SendOrNotFoundAsync_WithEmptyArray_ThrowsNotFoundException()
|
||||
public void GetOrThrow_NotFound_WithEmptyArray_ThrowsNotFoundException()
|
||||
{
|
||||
var mediator = CreateMediator<int[]>(Array.Empty<int>());
|
||||
var sender = CreateSender<int[]>(Array.Empty<int>());
|
||||
var request = new StubRequest<int[]?>();
|
||||
|
||||
Assert.ThrowsAsync<NotFoundException>(() => mediator.SendOrNotFoundAsync(request));
|
||||
Assert.ThrowsAsync<NotFoundException>(() => sender.GetOrThrow(request));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SendOrNotFoundAsync_WithEmptyCollection_AndCustomMessage_ThrowsNotFoundExceptionWithMessage()
|
||||
public void GetOrThrow_NotFound_WithEmptyCollection_AndCustomMessage_ContainsMessage()
|
||||
{
|
||||
const string message = "No items found";
|
||||
var mediator = CreateMediator<List<int>>(new List<int>());
|
||||
var sender = CreateSender<List<int>>(new List<int>());
|
||||
var request = new StubRequest<List<int>?>();
|
||||
|
||||
var ex = Assert.ThrowsAsync<NotFoundException>(
|
||||
() => mediator.SendOrNotFoundAsync(request, message, CancellationToken.None));
|
||||
() => sender.GetOrThrow(request, message, CancellationToken.None));
|
||||
|
||||
Assert.That(ex!.Message, Does.Contain(message));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SendOrNotFoundAsync — non-empty collection
|
||||
#region GetOrThrow<TResponse> (NotFoundException) — non-empty collection
|
||||
|
||||
[Test]
|
||||
public async Task SendOrNotFoundAsync_WithNonEmptyList_ReturnsResponse()
|
||||
public async Task GetOrThrow_NotFound_WithNonEmptyList_ReturnsResponse()
|
||||
{
|
||||
var expected = new List<string> { "a", "b" };
|
||||
var mediator = CreateMediator<List<string>>(expected);
|
||||
var sender = CreateSender<List<string>>(expected);
|
||||
var request = new StubRequest<List<string>?>();
|
||||
|
||||
var result = await mediator.SendOrNotFoundAsync(request);
|
||||
var result = await sender.GetOrThrow(request);
|
||||
|
||||
Assert.That(result, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SendOrNotFoundAsync_WithNonEmptyArray_ReturnsResponse()
|
||||
public async Task GetOrThrow_NotFound_WithNonEmptyArray_ReturnsResponse()
|
||||
{
|
||||
var expected = new[] { 1, 2, 3 };
|
||||
var mediator = CreateMediator<int[]>(expected);
|
||||
var sender = CreateSender<int[]>(expected);
|
||||
var request = new StubRequest<int[]?>();
|
||||
|
||||
var result = await mediator.SendOrNotFoundAsync(request);
|
||||
var result = await sender.GetOrThrow(request);
|
||||
|
||||
Assert.That(result, Is.EqualTo(expected));
|
||||
}
|
||||
@@ -260,27 +254,27 @@ public class MediatorExtensionsTests
|
||||
#region CancellationToken
|
||||
|
||||
[Test]
|
||||
public void SendOrThrowAsync_WithCancelledToken_ThrowsOperationCanceledException()
|
||||
public void GetOrThrow_WithCancelledToken_ThrowsOperationCanceledException()
|
||||
{
|
||||
var mediator = CreateMediator<string>("value");
|
||||
var sender = CreateSender<string>("value");
|
||||
var request = new StubRequest<string?>();
|
||||
var cts = new CancellationTokenSource();
|
||||
cts.Cancel();
|
||||
|
||||
Assert.ThrowsAsync<OperationCanceledException>(
|
||||
() => mediator.SendOrThrowAsync(request, () => new InvalidOperationException(), cts.Token));
|
||||
() => sender.GetOrThrow(request, () => new InvalidOperationException(), cts.Token));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SendOrNotFoundAsync_WithCancelledToken_ThrowsOperationCanceledException()
|
||||
public void GetOrThrow_NotFound_WithCancelledToken_ThrowsOperationCanceledException()
|
||||
{
|
||||
var mediator = CreateMediator<string>("value");
|
||||
var sender = CreateSender<string>("value");
|
||||
var request = new StubRequest<string?>();
|
||||
var cts = new CancellationTokenSource();
|
||||
cts.Cancel();
|
||||
|
||||
Assert.ThrowsAsync<OperationCanceledException>(
|
||||
() => mediator.SendOrNotFoundAsync(request, cts.Token));
|
||||
() => sender.GetOrThrow(request, cts.Token));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user