159 lines
4.2 KiB
C#
159 lines
4.2 KiB
C#
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using NUnit.Framework;
|
|
using NSubstitute;
|
|
|
|
namespace YourNamespace.Tests
|
|
{
|
|
[TestFixture]
|
|
public class ServiceShould
|
|
{
|
|
private IServiceDependency _dependency;
|
|
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
_dependency = Substitute.For<IServiceDependency>();
|
|
}
|
|
|
|
[Test]
|
|
public async Task ReturnResult_WhenOperationCompletes()
|
|
{
|
|
var expected = new ServiceResult { Value = 42 };
|
|
_dependency.GetDataAsync().Returns(Task.FromResult(expected));
|
|
var sut = CreateSut();
|
|
|
|
var result = await sut.PerformOperationAsync();
|
|
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result.Value, Is.EqualTo(42));
|
|
}
|
|
|
|
[Test]
|
|
public async Task CallDependency_WhenOperationIsPerformed()
|
|
{
|
|
var sut = CreateSut();
|
|
|
|
await sut.PerformOperationAsync();
|
|
|
|
await _dependency.Received(1).GetDataAsync();
|
|
}
|
|
|
|
[Test]
|
|
public async Task InvokeCallback_WhenOperationCompletes()
|
|
{
|
|
var callbackInvoked = false;
|
|
var sut = CreateSut();
|
|
sut.OnCompleted += () => callbackInvoked = true;
|
|
|
|
await sut.PerformOperationAsync();
|
|
|
|
Assert.That(callbackInvoked, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public async Task InvokeCallbackWithError_WhenOperationFails()
|
|
{
|
|
_dependency.GetDataAsync().Returns(Task.FromException<ServiceResult>(new System.Exception("Test error")));
|
|
var errorCallbackInvoked = false;
|
|
var sut = CreateSut();
|
|
sut.OnError += (ex) => errorCallbackInvoked = true;
|
|
|
|
try
|
|
{
|
|
await sut.PerformOperationAsync();
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
|
|
Assert.That(errorCallbackInvoked, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void ThrowException_WhenNotInitialized()
|
|
{
|
|
var sut = new TestService(null);
|
|
|
|
Assert.ThrowsAsync<System.InvalidOperationException>(() => sut.PerformOperationAsync());
|
|
}
|
|
|
|
[Test]
|
|
public async Task CancelOperation_WhenCancellationTokenIsCancelled()
|
|
{
|
|
var cts = new CancellationTokenSource();
|
|
cts.Cancel();
|
|
var sut = CreateSut();
|
|
|
|
Assert.ThrowsAsync<OperationCanceledException>(() => sut.PerformOperationAsync(cts.Token));
|
|
}
|
|
|
|
[Test]
|
|
public async Task SupportMultipleConcurrentOperations()
|
|
{
|
|
var sut = CreateSut();
|
|
|
|
var tasks = Enumerable.Range(0, 5)
|
|
.Select(_ => sut.PerformOperationAsync())
|
|
.ToArray();
|
|
|
|
var results = await Task.WhenAll(tasks);
|
|
|
|
Assert.That(results.Length, Is.EqualTo(5));
|
|
Assert.That(results.All(r => r != null), Is.True);
|
|
}
|
|
|
|
private TestService CreateSut()
|
|
{
|
|
return new TestService(_dependency);
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
}
|
|
}
|
|
|
|
public interface IServiceDependency
|
|
{
|
|
Task<ServiceResult> GetDataAsync();
|
|
}
|
|
|
|
public class ServiceResult
|
|
{
|
|
public int Value { get; set; }
|
|
}
|
|
|
|
public class TestService
|
|
{
|
|
private readonly IServiceDependency _dependency;
|
|
public event Action OnCompleted;
|
|
public event Action<System.Exception> OnError;
|
|
|
|
public TestService(IServiceDependency dependency)
|
|
{
|
|
_dependency = dependency;
|
|
}
|
|
|
|
public async Task<ServiceResult> PerformOperationAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
if (_dependency == null)
|
|
throw new System.InvalidOperationException("Service not initialized");
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
try
|
|
{
|
|
var result = await _dependency.GetDataAsync();
|
|
OnCompleted?.Invoke();
|
|
return result;
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
OnError?.Invoke(ex);
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|