Skip to content

Commit

Permalink
attempt at making imageservice tests less flaky
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsPilgaard committed Sep 19, 2023
1 parent 396405c commit 0969bac
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions tests/web/Jordnaer.Server.Tests/Profile/ImageServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,73 @@
using FluentAssertions;
using Jordnaer.Server.Features.Profile;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;

namespace Jordnaer.Server.Tests.Profile;

[Trait("Category", "IntegrationTest")]
[Collection(nameof(JordnaerWebApplicationFactoryCollection))]
public class ImageServiceTests
public class ImageService_Should
{
private readonly BlobServiceClient _blobServiceClient;
private readonly ImageService _sut;
private readonly IImageService _sut;

private const string ContainerName = "test-container";
private const string BlobName = "test-blob";

public ImageServiceTests(JordnaerWebApplicationFactory factory)
public ImageService_Should(JordnaerWebApplicationFactory factory)
{
_blobServiceClient = factory.Services.GetRequiredService<BlobServiceClient>();
var logger = factory.Services.GetRequiredService<ILogger<ImageService>>();

_sut = new ImageService(_blobServiceClient, logger);
_sut = factory.Services.GetRequiredService<IImageService>();

Check failure on line 21 in tests/web/Jordnaer.Server.Tests/Profile/ImageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Test Report

Jordnaer.Server.Tests.Profile.ImageService_Should ► DeleteImage_Successfully

Failed test found in: tests/web/Jordnaer.Server.Tests/TestResults/test-results.trx Error: System.InvalidOperationException : Cannot resolve scoped service 'Jordnaer.Server.Features.Profile.IImageService' from root provider.
Raw output
System.InvalidOperationException : Cannot resolve scoped service 'Jordnaer.Server.Features.Profile.IImageService' from root provider.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, IServiceScope scope, IServiceScope rootScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Jordnaer.Server.Tests.Profile.ImageService_Should..ctor(JordnaerWebApplicationFactory factory) in /home/runner/work/Jordnaer/Jordnaer/tests/web/Jordnaer.Server.Tests/Profile/ImageServiceTests.cs:line 21
   at InvokeStub_ImageService_Should..ctor(Object, Object, IntPtr*)
   at System.Reflection.ConstructorInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

Check failure on line 21 in tests/web/Jordnaer.Server.Tests/Profile/ImageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Test Report

Jordnaer.Server.Tests.Profile.ImageService_Should ► UploadImage_OverridesExistingBlob_Successfully

Failed test found in: tests/web/Jordnaer.Server.Tests/TestResults/test-results.trx Error: System.InvalidOperationException : Cannot resolve scoped service 'Jordnaer.Server.Features.Profile.IImageService' from root provider.
Raw output
System.InvalidOperationException : Cannot resolve scoped service 'Jordnaer.Server.Features.Profile.IImageService' from root provider.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, IServiceScope scope, IServiceScope rootScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Jordnaer.Server.Tests.Profile.ImageService_Should..ctor(JordnaerWebApplicationFactory factory) in /home/runner/work/Jordnaer/Jordnaer/tests/web/Jordnaer.Server.Tests/Profile/ImageServiceTests.cs:line 21
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.ConstructorInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

Check failure on line 21 in tests/web/Jordnaer.Server.Tests/Profile/ImageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Test Report

Jordnaer.Server.Tests.Profile.ImageService_Should ► UploadImage_UsingByteArray_Successfully

Failed test found in: tests/web/Jordnaer.Server.Tests/TestResults/test-results.trx Error: System.InvalidOperationException : Cannot resolve scoped service 'Jordnaer.Server.Features.Profile.IImageService' from root provider.
Raw output
System.InvalidOperationException : Cannot resolve scoped service 'Jordnaer.Server.Features.Profile.IImageService' from root provider.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, IServiceScope scope, IServiceScope rootScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Jordnaer.Server.Tests.Profile.ImageService_Should..ctor(JordnaerWebApplicationFactory factory) in /home/runner/work/Jordnaer/Jordnaer/tests/web/Jordnaer.Server.Tests/Profile/ImageServiceTests.cs:line 21
   at InvokeStub_ImageService_Should..ctor(Object, Object, IntPtr*)
   at System.Reflection.ConstructorInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

Check failure on line 21 in tests/web/Jordnaer.Server.Tests/Profile/ImageServiceTests.cs

View workflow job for this annotation

GitHub Actions / Test Report

Jordnaer.Server.Tests.Profile.ImageService_Should ► UploadImage_UsingFileStream_Successfully

Failed test found in: tests/web/Jordnaer.Server.Tests/TestResults/test-results.trx Error: System.InvalidOperationException : Cannot resolve scoped service 'Jordnaer.Server.Features.Profile.IImageService' from root provider.
Raw output
System.InvalidOperationException : Cannot resolve scoped service 'Jordnaer.Server.Features.Profile.IImageService' from root provider.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, IServiceScope scope, IServiceScope rootScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Jordnaer.Server.Tests.Profile.ImageService_Should..ctor(JordnaerWebApplicationFactory factory) in /home/runner/work/Jordnaer/Jordnaer/tests/web/Jordnaer.Server.Tests/Profile/ImageServiceTests.cs:line 21
   at InvokeStub_ImageService_Should..ctor(Object, Object, IntPtr*)
   at System.Reflection.ConstructorInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
}

[Fact]
public async Task UploadImage_UsingFileStream_Successfully()
{
// Arrange
string blobName = nameof(UploadImage_UsingFileStream_Successfully);
var fileStream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });

// Act
string result = await _sut.UploadImageAsync(BlobName, ContainerName, fileStream);
string result = await _sut.UploadImageAsync(blobName, ContainerName, fileStream);

// Assert
result.Should().NotBeNullOrEmpty();

// Clean up (delete the blob created during the test)
await _sut.DeleteImageAsync(BlobName, ContainerName);
await _sut.DeleteImageAsync(blobName, ContainerName);
}

[Fact]
public async Task UploadImage_UsingByteArray_Successfully()
{
// Arrange
string blobName = nameof(UploadImage_UsingByteArray_Successfully);
byte[] fileBytes = { 1, 2, 3, 4, 5 };

// Act
string result = await _sut.UploadImageAsync(BlobName, ContainerName, fileBytes);
string result = await _sut.UploadImageAsync(blobName, ContainerName, fileBytes);

// Assert
result.Should().NotBeNullOrEmpty();

// Clean up (delete the blob created during the test)
await _sut.DeleteImageAsync(BlobName, ContainerName);
await _sut.DeleteImageAsync(blobName, ContainerName);
}

[Fact]
public async Task DeleteImage_Successfully()
{
// Arrange
string blobName = nameof(DeleteImage_Successfully);
byte[] fileBytes = { 1, 2, 3, 4, 5 };
await _sut.UploadImageAsync(BlobName, ContainerName, fileBytes);
await _sut.UploadImageAsync(blobName, ContainerName, fileBytes);

// Act
await _sut.DeleteImageAsync(BlobName, ContainerName);
await _sut.DeleteImageAsync(blobName, ContainerName);

// Assert
var blobExists = await _blobServiceClient.GetBlobContainerClient(ContainerName)
.GetBlobClient(BlobName)
.GetBlobClient(blobName)
.ExistsAsync();
blobExists.Value.Should().BeFalse();
}
Expand All @@ -78,29 +77,30 @@ public async Task DeleteImage_Successfully()
public async Task UploadImage_OverridesExistingBlob_Successfully()
{
// Arrange
string blobName = nameof(UploadImage_OverridesExistingBlob_Successfully);
byte[] initialBytes = { 1, 2, 3, 4, 5 };
byte[] newBytes = { 6, 7, 8, 9, 10 };
await _sut.UploadImageAsync(BlobName, ContainerName, initialBytes);
await _sut.UploadImageAsync(blobName, ContainerName, initialBytes);

// Assert that the initial bytes are stored
var blob = await _blobServiceClient.GetBlobContainerClient(ContainerName)
.GetBlobClient(BlobName)
.GetBlobClient(blobName)
.DownloadContentAsync();
blob.Value.Content.ToArray().Should().BeEquivalentTo(initialBytes);

// Act
string result = await _sut.UploadImageAsync(BlobName, ContainerName, newBytes);
string result = await _sut.UploadImageAsync(blobName, ContainerName, newBytes);

// Assert
result.Should().NotBeNullOrEmpty();

// Assert that the new bytes are stored
blob = await _blobServiceClient.GetBlobContainerClient(ContainerName)
.GetBlobClient(BlobName)
.GetBlobClient(blobName)
.DownloadContentAsync();
blob.Value.Content.ToArray().Should().BeEquivalentTo(newBytes);

// Clean up (delete the blob created during the test)
await _sut.DeleteImageAsync(BlobName, ContainerName);
await _sut.DeleteImageAsync(blobName, ContainerName);
}
}

0 comments on commit 0969bac

Please sign in to comment.