diff --git a/.github/workflows/pr-validation-maui.yml b/.github/workflows/pr-validation-maui.yml index c6b96d36c..2f63a0943 100644 --- a/.github/workflows/pr-validation-maui.yml +++ b/.github/workflows/pr-validation-maui.yml @@ -39,4 +39,4 @@ jobs: run: dotnet build src/ClientApp/ClientApp.csproj - name: Test - run: dotnet test tests/ClientApp.UnitTests/ClientApp.UnitTests.csproj \ No newline at end of file + run: dotnet test --project tests/ClientApp.UnitTests/ClientApp.UnitTests.csproj --no-progress --output detailed \ No newline at end of file diff --git a/.github/workflows/pr-validation.yml b/.github/workflows/pr-validation.yml index 6da8ebc50..77431a0ca 100644 --- a/.github/workflows/pr-validation.yml +++ b/.github/workflows/pr-validation.yml @@ -26,4 +26,4 @@ jobs: - name: Build run: dotnet build eShop.Web.slnf - name: Test - run: dotnet test eShop.Web.slnf \ No newline at end of file + run: dotnet test --solution eShop.Web.slnf --no-build --no-progress --output detailed \ No newline at end of file diff --git a/Directory.Packages.props b/Directory.Packages.props index be19d5d32..91fd7fdcc 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -45,8 +45,7 @@ - - + @@ -76,8 +75,6 @@ - - @@ -91,8 +88,7 @@ - - + diff --git a/global.json b/global.json index 55e0e2cf3..c953d7d95 100644 --- a/global.json +++ b/global.json @@ -1,7 +1,13 @@ { "sdk": { - "version": "10.0.100-rc.1.25451.107", + "version": "10.0.100", "rollForward": "latestFeature", "allowPrerelease": true + }, + "test": { + "runner": "Microsoft.Testing.Platform" + }, + "msbuild-sdks": { + "MSTest.Sdk": "4.0.2" } } diff --git a/tests/Basket.UnitTests/Basket.UnitTests.csproj b/tests/Basket.UnitTests/Basket.UnitTests.csproj index 1d5601b84..a0c539f07 100644 --- a/tests/Basket.UnitTests/Basket.UnitTests.csproj +++ b/tests/Basket.UnitTests/Basket.UnitTests.csproj @@ -1,18 +1,16 @@ - + net10.0 false false false + Exe - - - all diff --git a/tests/Basket.UnitTests/BasketServiceTests.cs b/tests/Basket.UnitTests/BasketServiceTests.cs index 4e8abb4dc..ad44ab8f0 100644 --- a/tests/Basket.UnitTests/BasketServiceTests.cs +++ b/tests/Basket.UnitTests/BasketServiceTests.cs @@ -11,18 +11,20 @@ namespace eShop.Basket.UnitTests; [TestClass] public class BasketServiceTests { + public TestContext TestContext { get; set; } + [TestMethod] public async Task GetBasketReturnsEmptyForNoUser() { var mockRepository = Substitute.For(); var service = new BasketService(mockRepository, NullLogger.Instance); - var serverCallContext = TestServerCallContext.Create(); + var serverCallContext = TestServerCallContext.Create(cancellationToken: TestContext.CancellationToken); serverCallContext.SetUserState("__HttpContext", new DefaultHttpContext()); var response = await service.GetBasket(new GetBasketRequest(), serverCallContext); Assert.IsInstanceOfType(response); - Assert.AreEqual(response.Items.Count(), 0); + Assert.IsEmpty(response.Items); } [TestMethod] @@ -32,7 +34,7 @@ public async Task GetBasketReturnsItemsForValidUserId() List items = [new BasketItem { Id = "some-id" }]; mockRepository.GetBasketAsync("1").Returns(Task.FromResult(new CustomerBasket { BuyerId = "1", Items = items })); var service = new BasketService(mockRepository, NullLogger.Instance); - var serverCallContext = TestServerCallContext.Create(); + var serverCallContext = TestServerCallContext.Create(cancellationToken: TestContext.CancellationToken); var httpContext = new DefaultHttpContext(); httpContext.User = new ClaimsPrincipal(new ClaimsIdentity([new Claim("sub", "1")])); serverCallContext.SetUserState("__HttpContext", httpContext); @@ -40,7 +42,7 @@ public async Task GetBasketReturnsItemsForValidUserId() var response = await service.GetBasket(new GetBasketRequest(), serverCallContext); Assert.IsInstanceOfType(response); - Assert.AreEqual(response.Items.Count(), 1); + Assert.HasCount(1, response.Items); } [TestMethod] @@ -50,13 +52,13 @@ public async Task GetBasketReturnsEmptyForInvalidUserId() List items = [new BasketItem { Id = "some-id" }]; mockRepository.GetBasketAsync("1").Returns(Task.FromResult(new CustomerBasket { BuyerId = "1", Items = items })); var service = new BasketService(mockRepository, NullLogger.Instance); - var serverCallContext = TestServerCallContext.Create(); + var serverCallContext = TestServerCallContext.Create(cancellationToken: TestContext.CancellationToken); var httpContext = new DefaultHttpContext(); serverCallContext.SetUserState("__HttpContext", httpContext); var response = await service.GetBasket(new GetBasketRequest(), serverCallContext); Assert.IsInstanceOfType(response); - Assert.AreEqual(response.Items.Count(), 0); + Assert.IsEmpty(response.Items); } } diff --git a/tests/Basket.UnitTests/GlobalUsings.cs b/tests/Basket.UnitTests/GlobalUsings.cs index 66d5613a6..e97b95599 100644 --- a/tests/Basket.UnitTests/GlobalUsings.cs +++ b/tests/Basket.UnitTests/GlobalUsings.cs @@ -1,6 +1,10 @@ -global using System.Collections.Generic; +global using System; +global using System.Collections.Generic; +global using System.Threading; global using System.Threading.Tasks; global using Microsoft.AspNetCore.Http; global using Microsoft.AspNetCore.Mvc; global using NSubstitute; global using Microsoft.VisualStudio.TestTools.UnitTesting; + +[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)] diff --git a/tests/Basket.UnitTests/Helpers/TestServerCallContext.cs b/tests/Basket.UnitTests/Helpers/TestServerCallContext.cs index 00f4bc4c5..cecf4781d 100644 --- a/tests/Basket.UnitTests/Helpers/TestServerCallContext.cs +++ b/tests/Basket.UnitTests/Helpers/TestServerCallContext.cs @@ -1,4 +1,4 @@ -using Grpc.Core; +using Grpc.Core; namespace eShop.Basket.UnitTests.Helpers; diff --git a/tests/Catalog.FunctionalTests/Catalog.FunctionalTests.csproj b/tests/Catalog.FunctionalTests/Catalog.FunctionalTests.csproj index 872a6d058..2b67f43b7 100644 --- a/tests/Catalog.FunctionalTests/Catalog.FunctionalTests.csproj +++ b/tests/Catalog.FunctionalTests/Catalog.FunctionalTests.csproj @@ -4,6 +4,7 @@ net10.0 false false + Exe @@ -11,12 +12,7 @@ - - - all - runtime; build; native; contentfiles; analyzers - - + diff --git a/tests/Catalog.FunctionalTests/CatalogApiFixture.cs b/tests/Catalog.FunctionalTests/CatalogApiFixture.cs index 2471477ef..dca044f8f 100644 --- a/tests/Catalog.FunctionalTests/CatalogApiFixture.cs +++ b/tests/Catalog.FunctionalTests/CatalogApiFixture.cs @@ -1,4 +1,8 @@ using System.Reflection; + +using Aspire.Hosting; +using Aspire.Hosting.ApplicationModel; + using Microsoft.AspNetCore.Mvc.Testing; namespace eShop.Catalog.FunctionalTests; @@ -46,7 +50,7 @@ protected override IHost CreateHost(IHostBuilder builder) } } - public async Task InitializeAsync() + public async ValueTask InitializeAsync() { await _app.StartAsync(); _postgresConnectionString = await Postgres.Resource.GetConnectionStringAsync(); diff --git a/tests/Catalog.FunctionalTests/CatalogApiTests.cs b/tests/Catalog.FunctionalTests/CatalogApiTests.cs index fcd91179e..c29bfa6c9 100644 --- a/tests/Catalog.FunctionalTests/CatalogApiTests.cs +++ b/tests/Catalog.FunctionalTests/CatalogApiTests.cs @@ -1,4 +1,4 @@ -using System.Net.Http.Json; +using System.Net.Http.Json; using System.Text.Json; using Asp.Versioning; using Asp.Versioning.Http; @@ -31,15 +31,15 @@ public async Task GetCatalogItemsRespectsPageSize(double version) var _httpClient = CreateHttpClient(new ApiVersion(version)); // Act - var response = await _httpClient.GetAsync("/api/catalog/items?pageIndex=0&pageSize=5"); + var response = await _httpClient.GetAsync("/api/catalog/items?pageIndex=0&pageSize=5", TestContext.Current.CancellationToken); // Assert response.EnsureSuccessStatusCode(); - var body = await response.Content.ReadAsStringAsync(); + var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var result = JsonSerializer.Deserialize>(body, _jsonSerializerOptions); - // Assert 12 total items with 5 retrieved from index 0 - Assert.Equal(101, result.Count); + // Assert 103 total items (101 seeded + 2 added by AddCatalogItem tests) with 5 retrieved from index 0 + Assert.Equal(103, result.Count); Assert.Equal(0, result.PageIndex); Assert.Equal(5, result.PageSize); } @@ -52,9 +52,9 @@ public async Task UpdateCatalogItemWorksWithoutPriceUpdate(double version) var _httpClient = CreateHttpClient(new ApiVersion(version)); // Act - 1 - var response = await _httpClient.GetAsync("/api/catalog/items/1"); + var response = await _httpClient.GetAsync("/api/catalog/items/1", TestContext.Current.CancellationToken); response.EnsureSuccessStatusCode(); - var body = await response.Content.ReadAsStringAsync(); + var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var itemToUpdate = JsonSerializer.Deserialize(body, _jsonSerializerOptions); // Act - 2 @@ -62,16 +62,16 @@ public async Task UpdateCatalogItemWorksWithoutPriceUpdate(double version) itemToUpdate.AvailableStock -= 1; response = version switch { - 1.0 => await _httpClient.PutAsJsonAsync("/api/catalog/items", itemToUpdate), - 2.0 => await _httpClient.PutAsJsonAsync($"/api/catalog/items/{itemToUpdate.Id}", itemToUpdate), + 1.0 => await _httpClient.PutAsJsonAsync("/api/catalog/items", itemToUpdate, TestContext.Current.CancellationToken), + 2.0 => await _httpClient.PutAsJsonAsync($"/api/catalog/items/{itemToUpdate.Id}", itemToUpdate, TestContext.Current.CancellationToken), _ => throw new ArgumentOutOfRangeException(nameof(version), version, null) }; response.EnsureSuccessStatusCode(); // Act - 3 - response = await _httpClient.GetAsync("/api/catalog/items/1"); + response = await _httpClient.GetAsync("/api/catalog/items/1", TestContext.Current.CancellationToken); response.EnsureSuccessStatusCode(); - body = await response.Content.ReadAsStringAsync(); + body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var updatedItem = JsonSerializer.Deserialize(body, _jsonSerializerOptions); // Assert - 1 @@ -87,9 +87,9 @@ public async Task UpdateCatalogItemWorksWithPriceUpdate(double version) var _httpClient = CreateHttpClient(new ApiVersion(version)); // Act - 1 - var response = await _httpClient.GetAsync("/api/catalog/items/1"); + var response = await _httpClient.GetAsync("/api/catalog/items/1", TestContext.Current.CancellationToken); response.EnsureSuccessStatusCode(); - var body = await response.Content.ReadAsStringAsync(); + var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var itemToUpdate = JsonSerializer.Deserialize(body, _jsonSerializerOptions); // Act - 2 @@ -98,16 +98,16 @@ public async Task UpdateCatalogItemWorksWithPriceUpdate(double version) itemToUpdate.Price = 1.99m; response = version switch { - 1.0 => await _httpClient.PutAsJsonAsync("/api/catalog/items", itemToUpdate), - 2.0 => await _httpClient.PutAsJsonAsync($"/api/catalog/items/{itemToUpdate.Id}", itemToUpdate), + 1.0 => await _httpClient.PutAsJsonAsync("/api/catalog/items", itemToUpdate, TestContext.Current.CancellationToken), + 2.0 => await _httpClient.PutAsJsonAsync($"/api/catalog/items/{itemToUpdate.Id}", itemToUpdate, TestContext.Current.CancellationToken), _ => throw new ArgumentOutOfRangeException(nameof(version), version, null) }; response.EnsureSuccessStatusCode(); // Act - 3 - response = await _httpClient.GetAsync("/api/catalog/items/1"); + response = await _httpClient.GetAsync("/api/catalog/items/1", TestContext.Current.CancellationToken); response.EnsureSuccessStatusCode(); - body = await response.Content.ReadAsStringAsync(); + body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var updatedItem = JsonSerializer.Deserialize(body, _jsonSerializerOptions); // Assert - 1 @@ -124,11 +124,11 @@ public async Task GetCatalogItemsbyIds(double version) var _httpClient = CreateHttpClient(new ApiVersion(version)); // Act - var response = await _httpClient.GetAsync("/api/catalog/items/by?ids=1&ids=2&ids=3"); + var response = await _httpClient.GetAsync("/api/catalog/items/by?ids=1&ids=2&ids=3", TestContext.Current.CancellationToken); // Arrange response.EnsureSuccessStatusCode(); - var body = await response.Content.ReadAsStringAsync(); + var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var result = JsonSerializer.Deserialize>(body, _jsonSerializerOptions); // Assert 3 items @@ -143,11 +143,11 @@ public async Task GetCatalogItemWithId(double version) var _httpClient = CreateHttpClient(new ApiVersion(version)); // Act - var response = await _httpClient.GetAsync("/api/catalog/items/2"); + var response = await _httpClient.GetAsync("/api/catalog/items/2", TestContext.Current.CancellationToken); // Arrange response.EnsureSuccessStatusCode(); - var body = await response.Content.ReadAsStringAsync(); + var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var result = JsonSerializer.Deserialize(body, _jsonSerializerOptions); // Assert @@ -165,14 +165,14 @@ public async Task GetCatalogItemWithExactName(double version) // Act var response = version switch { - 1.0 => await _httpClient.GetAsync("api/catalog/items/by/Wanderer%20Black%20Hiking%20Boots?PageSize=5&PageIndex=0"), - 2.0 => await _httpClient.GetAsync("api/catalog/items?name=Wanderer%20Black%20Hiking%20Boots&PageSize=5&PageIndex=0"), + 1.0 => await _httpClient.GetAsync("api/catalog/items/by/Wanderer%20Black%20Hiking%20Boots?PageSize=5&PageIndex=0", TestContext.Current.CancellationToken), + 2.0 => await _httpClient.GetAsync("api/catalog/items?name=Wanderer%20Black%20Hiking%20Boots&PageSize=5&PageIndex=0", TestContext.Current.CancellationToken), _ => throw new ArgumentOutOfRangeException(nameof(version), version, null) }; // Arrange response.EnsureSuccessStatusCode(); - var body = await response.Content.ReadAsStringAsync(); + var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var result = JsonSerializer.Deserialize>(body, _jsonSerializerOptions); // Assert @@ -193,14 +193,14 @@ public async Task GetCatalogItemWithPartialName(double version) // Act var response = version switch { - 1.0 => await _httpClient.GetAsync("api/catalog/items/by/Alpine?PageSize=5&PageIndex=0"), - 2.0 => await _httpClient.GetAsync("api/catalog/items?name=Alpine&PageSize=5&PageIndex=0"), + 1.0 => await _httpClient.GetAsync("api/catalog/items/by/Alpine?PageSize=5&PageIndex=0", TestContext.Current.CancellationToken), + 2.0 => await _httpClient.GetAsync("api/catalog/items?name=Alpine&PageSize=5&PageIndex=0", TestContext.Current.CancellationToken), _ => throw new ArgumentOutOfRangeException(nameof(version), version, null) }; // Arrange response.EnsureSuccessStatusCode(); - var body = await response.Content.ReadAsStringAsync(); + var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var result = JsonSerializer.Deserialize>(body, _jsonSerializerOptions); // Assert @@ -219,7 +219,7 @@ public async Task GetCatalogItemPicWithId(double version) var _httpClient = CreateHttpClient(new ApiVersion(version)); // Act - var response = await _httpClient.GetAsync("api/catalog/items/1/pic"); + var response = await _httpClient.GetAsync("api/catalog/items/1/pic", TestContext.Current.CancellationToken); // Arrange response.EnsureSuccessStatusCode(); @@ -239,14 +239,14 @@ public async Task GetCatalogItemWithsemanticrelevance(double version) // Act var response = version switch { - 1.0 => await _httpClient.GetAsync("api/catalog/items/withsemanticrelevance/Wanderer?PageSize=5&PageIndex=0"), - 2.0 => await _httpClient.GetAsync("api/catalog/items/withsemanticrelevance?text=Wanderer&PageSize=5&PageIndex=0"), + 1.0 => await _httpClient.GetAsync("api/catalog/items/withsemanticrelevance/Wanderer?PageSize=5&PageIndex=0", TestContext.Current.CancellationToken), + 2.0 => await _httpClient.GetAsync("api/catalog/items/withsemanticrelevance?text=Wanderer&PageSize=5&PageIndex=0", TestContext.Current.CancellationToken), _ => throw new ArgumentOutOfRangeException(nameof(version), version, null) }; // Arrange response.EnsureSuccessStatusCode(); - var body = await response.Content.ReadAsStringAsync(); + var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var result = JsonSerializer.Deserialize>(body, _jsonSerializerOptions); // Assert @@ -266,14 +266,14 @@ public async Task GetCatalogItemWithTypeIdBrandId(double version) // Act var response = version switch { - 1.0 => await _httpClient.GetAsync("api/catalog/items/type/3/brand/3?PageSize=5&PageIndex=0"), - 2.0 => await _httpClient.GetAsync("api/catalog/items?type=3&brand=3&PageSize=5&PageIndex=0"), + 1.0 => await _httpClient.GetAsync("api/catalog/items/type/3/brand/3?PageSize=5&PageIndex=0", TestContext.Current.CancellationToken), + 2.0 => await _httpClient.GetAsync("api/catalog/items?type=3&brand=3&PageSize=5&PageIndex=0", TestContext.Current.CancellationToken), _ => throw new ArgumentOutOfRangeException(nameof(version), version, null) }; // Arrange response.EnsureSuccessStatusCode(); - var body = await response.Content.ReadAsStringAsync(); + var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var result = JsonSerializer.Deserialize>(body, _jsonSerializerOptions); // Assert @@ -295,14 +295,14 @@ public async Task GetAllCatalogTypeItemWithBrandId(double version) // Act var response = version switch { - 1.0 => await _httpClient.GetAsync("api/catalog/items/type/all/brand/3?PageSize=5&PageIndex=0"), - 2.0 => await _httpClient.GetAsync("api/catalog/items?brand=3&PageSize=5&PageIndex=0"), + 1.0 => await _httpClient.GetAsync("api/catalog/items/type/all/brand/3?PageSize=5&PageIndex=0", TestContext.Current.CancellationToken), + 2.0 => await _httpClient.GetAsync("api/catalog/items?brand=3&PageSize=5&PageIndex=0", TestContext.Current.CancellationToken), _ => throw new ArgumentOutOfRangeException(nameof(version), version, null) }; // Arrange response.EnsureSuccessStatusCode(); - var body = await response.Content.ReadAsStringAsync(); + var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var result = JsonSerializer.Deserialize>(body, _jsonSerializerOptions); // Assert @@ -321,11 +321,11 @@ public async Task GetAllCatalogTypes(double version) var _httpClient = CreateHttpClient(new ApiVersion(version)); // Act - var response = await _httpClient.GetAsync("api/catalog/catalogtypes"); + var response = await _httpClient.GetAsync("api/catalog/catalogtypes", TestContext.Current.CancellationToken); // Arrange response.EnsureSuccessStatusCode(); - var body = await response.Content.ReadAsStringAsync(); + var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var result = JsonSerializer.Deserialize>(body, _jsonSerializerOptions); // Assert @@ -341,11 +341,11 @@ public async Task GetAllCatalogBrands(double version) var _httpClient = CreateHttpClient(new ApiVersion(version)); // Act - var response = await _httpClient.GetAsync("api/catalog/catalogbrands"); + var response = await _httpClient.GetAsync("api/catalog/catalogbrands", TestContext.Current.CancellationToken); // Arrange response.EnsureSuccessStatusCode(); - var body = await response.Content.ReadAsStringAsync(); + var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var result = JsonSerializer.Deserialize>(body, _jsonSerializerOptions); // Assert @@ -381,13 +381,13 @@ public async Task AddCatalogItem(double version) MaxStockThreshold = 200, OnReorder = false }; - var response = await _httpClient.PostAsJsonAsync("/api/catalog/items", bodyContent); + var response = await _httpClient.PostAsJsonAsync("/api/catalog/items", bodyContent, TestContext.Current.CancellationToken); response.EnsureSuccessStatusCode(); // Act - 2 - response = await _httpClient.GetAsync($"/api/catalog/items/{id}"); + response = await _httpClient.GetAsync($"/api/catalog/items/{id}", TestContext.Current.CancellationToken); response.EnsureSuccessStatusCode(); - var body = await response.Content.ReadAsStringAsync(); + var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var addedItem = JsonSerializer.Deserialize(body, _jsonSerializerOptions); // Assert - 1 @@ -409,11 +409,11 @@ public async Task DeleteCatalogItem(double version) }; //Act - 1 - var response = await _httpClient.DeleteAsync($"/api/catalog/items/{id}"); + var response = await _httpClient.DeleteAsync($"/api/catalog/items/{id}", TestContext.Current.CancellationToken); response.EnsureSuccessStatusCode(); // Act - 2 - var response1 = await _httpClient.GetAsync($"/api/catalog/items/{id}"); + var response1 = await _httpClient.GetAsync($"/api/catalog/items/{id}", TestContext.Current.CancellationToken); var responseStatus = response1.StatusCode; // Assert - 1 diff --git a/tests/Catalog.FunctionalTests/GlobalUsings.cs b/tests/Catalog.FunctionalTests/GlobalUsings.cs index 58dbf6ade..a82218dbe 100644 --- a/tests/Catalog.FunctionalTests/GlobalUsings.cs +++ b/tests/Catalog.FunctionalTests/GlobalUsings.cs @@ -1,5 +1,7 @@ -global using System.IO; -global using System.Net; +global using System; +global using System.Collections.Generic; +global using System.Linq; +global using System.Net.Http; global using System.Threading.Tasks; global using Microsoft.AspNetCore.Hosting; global using Microsoft.AspNetCore.TestHost; diff --git a/tests/ClientApp.UnitTests/ClientApp.UnitTests.csproj b/tests/ClientApp.UnitTests/ClientApp.UnitTests.csproj index b1789d8f0..fff29a3fe 100644 --- a/tests/ClientApp.UnitTests/ClientApp.UnitTests.csproj +++ b/tests/ClientApp.UnitTests/ClientApp.UnitTests.csproj @@ -1,4 +1,4 @@ - + net10.0 @@ -6,6 +6,7 @@ enable false true + Exe false @@ -15,17 +16,6 @@ - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - diff --git a/tests/ClientApp.UnitTests/GlobalUsings.cs b/tests/ClientApp.UnitTests/GlobalUsings.cs index 3f9e1aba6..66ca8f8f9 100644 --- a/tests/ClientApp.UnitTests/GlobalUsings.cs +++ b/tests/ClientApp.UnitTests/GlobalUsings.cs @@ -6,3 +6,5 @@ global using eShop.ClientApp.Services.Settings; global using eShop.ClientApp.ViewModels; global using Microsoft.VisualStudio.TestTools.UnitTesting; + +[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)] diff --git a/tests/ClientApp.UnitTests/Services/BasketServiceTests.cs b/tests/ClientApp.UnitTests/Services/BasketServiceTests.cs index d124e03e7..e555671cf 100644 --- a/tests/ClientApp.UnitTests/Services/BasketServiceTests.cs +++ b/tests/ClientApp.UnitTests/Services/BasketServiceTests.cs @@ -8,6 +8,6 @@ public async Task GetFakeBasketTest() { var catalogMockService = new CatalogMockService(); var result = await catalogMockService.GetCatalogAsync(); - Assert.AreNotEqual(result.Count(), 0); + Assert.AreNotEqual(0, result.Count()); } } diff --git a/tests/ClientApp.UnitTests/Services/CatalogServiceTests.cs b/tests/ClientApp.UnitTests/Services/CatalogServiceTests.cs index 64a53bbc6..858c21f25 100644 --- a/tests/ClientApp.UnitTests/Services/CatalogServiceTests.cs +++ b/tests/ClientApp.UnitTests/Services/CatalogServiceTests.cs @@ -9,7 +9,7 @@ public async Task GetFakeCatalogTest() var catalogMockService = new CatalogMockService(); var catalog = await catalogMockService.GetCatalogAsync(); - Assert.AreNotEqual(catalog.Count(), 0); + Assert.AreNotEqual(0, catalog.Count()); } [TestMethod] @@ -18,7 +18,7 @@ public async Task GetFakeCatalogBrandTest() var catalogMockService = new CatalogMockService(); var catalogBrand = await catalogMockService.GetCatalogBrandAsync(); - Assert.AreNotEqual(catalogBrand.Count(), 0); + Assert.AreNotEqual(0, catalogBrand.Count()); } [TestMethod] @@ -27,6 +27,6 @@ public async Task GetFakeCatalogTypeTest() var catalogMockService = new CatalogMockService(); var catalogType = await catalogMockService.GetCatalogTypeAsync(); - Assert.AreNotEqual(catalogType.Count(), 0); + Assert.AreNotEqual(0, catalogType.Count()); } } diff --git a/tests/ClientApp.UnitTests/Services/OrdersServiceTests.cs b/tests/ClientApp.UnitTests/Services/OrdersServiceTests.cs index 9b67f7f09..4b5d7eab4 100644 --- a/tests/ClientApp.UnitTests/Services/OrdersServiceTests.cs +++ b/tests/ClientApp.UnitTests/Services/OrdersServiceTests.cs @@ -27,6 +27,6 @@ public async Task GetFakeOrdersTest() var ordersMockService = new OrderMockService(); var result = await ordersMockService.GetOrdersAsync(); - Assert.AreNotEqual(result.Count(), 0); + Assert.AreNotEqual(0, result.Count()); } } diff --git a/tests/ClientApp.UnitTests/ViewModels/CatalogViewModelTests.cs b/tests/ClientApp.UnitTests/ViewModels/CatalogViewModelTests.cs index e33e41e26..d89bee0d4 100644 --- a/tests/ClientApp.UnitTests/ViewModels/CatalogViewModelTests.cs +++ b/tests/ClientApp.UnitTests/ViewModels/CatalogViewModelTests.cs @@ -46,14 +46,14 @@ public void ClearFilterCommandIsNotNullTest() public void ProductsPropertyIsEmptyWhenViewModelInstantiatedTest() { var catalogViewModel = new CatalogViewModel(_appEnvironmentService, _navigationService); - Assert.AreEqual(catalogViewModel.Products.Count,0); + Assert.IsEmpty(catalogViewModel.Products); } [TestMethod] public void BrandsPropertyIsEmptyWhenViewModelInstantiatedTest() { var catalogViewModel = new CatalogViewModel(_appEnvironmentService, _navigationService); - Assert.AreEqual(catalogViewModel.Brands.Count, 0); + Assert.IsEmpty(catalogViewModel.Brands); } [TestMethod] @@ -67,7 +67,7 @@ public void BrandPropertyIsNullWhenViewModelInstantiatedTest() public void TypesPropertyIsEmptyWhenViewModelInstantiatedTest() { var catalogViewModel = new CatalogViewModel(_appEnvironmentService, _navigationService); - Assert.AreEqual(catalogViewModel.Types.Count, 0); + Assert.IsEmpty(catalogViewModel.Types); } [TestMethod] diff --git a/tests/ClientApp.UnitTests/ViewModels/MockViewModelTests.cs b/tests/ClientApp.UnitTests/ViewModels/MockViewModelTests.cs index 29e3f1749..f9da19cc8 100644 --- a/tests/ClientApp.UnitTests/ViewModels/MockViewModelTests.cs +++ b/tests/ClientApp.UnitTests/ViewModels/MockViewModelTests.cs @@ -24,8 +24,8 @@ public void CheckValidationFailsWhenPropertiesAreEmptyTest() Assert.IsNull(mockViewModel.Surname.Value); Assert.IsFalse(mockViewModel.Forename.IsValid); Assert.IsFalse(mockViewModel.Surname.IsValid); - Assert.AreNotEqual(mockViewModel.Forename.Errors.Count(), 0); - Assert.AreNotEqual(mockViewModel.Surname.Errors.Count(), 0); + Assert.AreNotEqual(0, mockViewModel.Forename.Errors.Count()); + Assert.AreNotEqual(0, mockViewModel.Surname.Errors.Count()); } [TestMethod] @@ -41,8 +41,8 @@ public void CheckValidationFailsWhenOnlyForenameHasDataTest() Assert.IsNull(mockViewModel.Surname.Value); Assert.IsTrue(mockViewModel.Forename.IsValid); Assert.IsFalse(mockViewModel.Surname.IsValid); - Assert.AreEqual(mockViewModel.Forename.Errors.Count(), 0); - Assert.AreNotEqual(mockViewModel.Surname.Errors.Count(), 0); + Assert.AreEqual(0, mockViewModel.Forename.Errors.Count()); + Assert.AreNotEqual(0, mockViewModel.Surname.Errors.Count()); } [TestMethod] @@ -58,8 +58,8 @@ public void CheckValidationPassesWhenOnlySurnameHasDataTest() Assert.IsNotNull(mockViewModel.Surname.Value); Assert.IsFalse(mockViewModel.Forename.IsValid); Assert.IsTrue(mockViewModel.Surname.IsValid); - Assert.AreNotEqual(mockViewModel.Forename.Errors.Count(), 0); - Assert.AreEqual(mockViewModel.Surname.Errors.Count(), 0); + Assert.AreNotEqual(0, mockViewModel.Forename.Errors.Count()); + Assert.AreEqual(0, mockViewModel.Surname.Errors.Count()); } [TestMethod] @@ -76,8 +76,8 @@ public void CheckValidationPassesWhenBothPropertiesHaveDataTest() Assert.IsNotNull(mockViewModel.Surname.Value); Assert.IsTrue(mockViewModel.Forename.IsValid); Assert.IsTrue(mockViewModel.Surname.IsValid); - Assert.AreEqual(mockViewModel.Forename.Errors.Count(), 0); - Assert.AreEqual(mockViewModel.Surname.Errors.Count(), 0); + Assert.AreEqual(0, mockViewModel.Forename.Errors.Count()); + Assert.AreEqual(0, mockViewModel.Surname.Errors.Count()); } [TestMethod] diff --git a/tests/Directory.Build.props b/tests/Directory.Build.props new file mode 100644 index 000000000..ef689d8df --- /dev/null +++ b/tests/Directory.Build.props @@ -0,0 +1,13 @@ + + + + + Recommended + + + + + true + + + \ No newline at end of file diff --git a/tests/Ordering.FunctionalTests/GlobalUsings.cs b/tests/Ordering.FunctionalTests/GlobalUsings.cs index 9ce089152..09fd6e57c 100644 --- a/tests/Ordering.FunctionalTests/GlobalUsings.cs +++ b/tests/Ordering.FunctionalTests/GlobalUsings.cs @@ -1,6 +1,9 @@ -global using System.IO; +global using System; +global using System.Collections.Generic; +global using System.Linq; global using System.Net.Http; global using System.Security.Claims; +global using System.Threading.Tasks; global using Microsoft.AspNetCore.Builder; global using Microsoft.AspNetCore.Hosting; global using Microsoft.AspNetCore.Http; diff --git a/tests/Ordering.FunctionalTests/Ordering.FunctionalTests.csproj b/tests/Ordering.FunctionalTests/Ordering.FunctionalTests.csproj index 153b731f9..422d88687 100644 --- a/tests/Ordering.FunctionalTests/Ordering.FunctionalTests.csproj +++ b/tests/Ordering.FunctionalTests/Ordering.FunctionalTests.csproj @@ -4,19 +4,15 @@ net10.0 false false + Exe - - - all - runtime; build; native; contentfiles; analyzers - - + diff --git a/tests/Ordering.FunctionalTests/OrderingApiFixture.cs b/tests/Ordering.FunctionalTests/OrderingApiFixture.cs index 236843fe2..9ca68abf3 100644 --- a/tests/Ordering.FunctionalTests/OrderingApiFixture.cs +++ b/tests/Ordering.FunctionalTests/OrderingApiFixture.cs @@ -1,4 +1,7 @@ -using Microsoft.AspNetCore.Mvc.Testing; +using Aspire.Hosting; +using Aspire.Hosting.ApplicationModel; + +using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.Hosting; namespace eShop.Ordering.FunctionalTests; @@ -54,7 +57,7 @@ protected override IHost CreateHost(IHostBuilder builder) } } - public async Task InitializeAsync() + public async ValueTask InitializeAsync() { await _app.StartAsync(); _postgresConnectionString = await Postgres.Resource.GetConnectionStringAsync(); diff --git a/tests/Ordering.FunctionalTests/OrderingApiTests.cs b/tests/Ordering.FunctionalTests/OrderingApiTests.cs index ee149c2ce..9b96d9de3 100644 --- a/tests/Ordering.FunctionalTests/OrderingApiTests.cs +++ b/tests/Ordering.FunctionalTests/OrderingApiTests.cs @@ -27,8 +27,8 @@ public OrderingApiTests(OrderingApiFixture fixture) public async Task GetAllStoredOrdersWorks() { // Act - var response = await _httpClient.GetAsync("api/orders"); - var s = await response.Content.ReadAsStringAsync(); + var response = await _httpClient.GetAsync("api/orders", TestContext.Current.CancellationToken); + var s = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); response.EnsureSuccessStatusCode(); // Assert @@ -43,8 +43,8 @@ public async Task CancelWithEmptyGuidFails() { Headers = { { "x-requestid", Guid.Empty.ToString() } } }; - var response = await _httpClient.PutAsync("/api/orders/cancel", content); - var s = await response.Content.ReadAsStringAsync(); + var response = await _httpClient.PutAsync("/api/orders/cancel", content, TestContext.Current.CancellationToken); + var s = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); // Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); @@ -58,8 +58,8 @@ public async Task CancelNonExistentOrderFails() { Headers = { { "x-requestid", Guid.NewGuid().ToString() } } }; - var response = await _httpClient.PutAsync("api/orders/cancel", content); - var s = await response.Content.ReadAsStringAsync(); + var response = await _httpClient.PutAsync("api/orders/cancel", content, TestContext.Current.CancellationToken); + var s = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); // Assert Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); @@ -73,8 +73,8 @@ public async Task ShipWithEmptyGuidFails() { Headers = { { "x-requestid", Guid.Empty.ToString() } } }; - var response = await _httpClient.PutAsync("api/orders/ship", content); - var s = await response.Content.ReadAsStringAsync(); + var response = await _httpClient.PutAsync("api/orders/ship", content, TestContext.Current.CancellationToken); + var s = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); // Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); @@ -88,7 +88,7 @@ public async Task ShipNonExistentOrderFails() { Headers = { { "x-requestid", Guid.NewGuid().ToString() } } }; - var response = await _httpClient.PutAsync("api/orders/ship", content); + var response = await _httpClient.PutAsync("api/orders/ship", content, TestContext.Current.CancellationToken); // Assert Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); @@ -98,8 +98,8 @@ public async Task ShipNonExistentOrderFails() public async Task GetAllOrdersCardType() { // Act 1 - var response = await _httpClient.GetAsync("api/orders/cardtypes"); - var s = await response.Content.ReadAsStringAsync(); + var response = await _httpClient.GetAsync("api/orders/cardtypes", TestContext.Current.CancellationToken); + var s = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); response.EnsureSuccessStatusCode(); // Assert @@ -110,7 +110,7 @@ public async Task GetAllOrdersCardType() public async Task GetStoredOrdersWithOrderId() { // Act - var response = await _httpClient.GetAsync("api/orders/1"); + var response = await _httpClient.GetAsync("api/orders/1", TestContext.Current.CancellationToken); var responseStatus = response.StatusCode; // Assert @@ -125,8 +125,8 @@ public async Task AddNewEmptyOrder() { Headers = { { "x-requestid", Guid.Empty.ToString() } } }; - var response = await _httpClient.PostAsync("api/orders", content); - var s = await response.Content.ReadAsStringAsync(); + var response = await _httpClient.PostAsync("api/orders", content, TestContext.Current.CancellationToken); + var s = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); // Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); @@ -152,8 +152,8 @@ public async Task AddNewOrder() { Headers = { { "x-requestid", Guid.NewGuid().ToString() } } }; - var response = await _httpClient.PostAsync("api/orders", content); - var s = await response.Content.ReadAsStringAsync(); + var response = await _httpClient.PostAsync("api/orders", content, TestContext.Current.CancellationToken); + var s = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -178,8 +178,8 @@ public async Task PostDraftOrder() { Headers = { { "x-requestid", Guid.NewGuid().ToString() } } }; - var response = await _httpClient.PostAsync("api/orders/draft", content); - var s = await response.Content.ReadAsStringAsync(); + var response = await _httpClient.PostAsync("api/orders/draft", content, TestContext.Current.CancellationToken); + var s = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); @@ -193,9 +193,9 @@ public async Task CreateOrderDraftSucceeds() { Headers = { { "x-requestid", Guid.NewGuid().ToString() } } }; - var response = await _httpClient.PostAsync("api/orders/draft", content); + var response = await _httpClient.PostAsync("api/orders/draft", content, TestContext.Current.CancellationToken); - var s = await response.Content.ReadAsStringAsync(); + var s = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var responseData = JsonSerializer.Deserialize(s, new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); Assert.Equal(HttpStatusCode.OK, response.StatusCode); diff --git a/tests/Ordering.UnitTests/Application/SetStockRejectedOrderStatusCommandTest.cs b/tests/Ordering.UnitTests/Application/SetStockRejectedOrderStatusCommandTest.cs index 5f9d8a867..6f8233b79 100644 --- a/tests/Ordering.UnitTests/Application/SetStockRejectedOrderStatusCommandTest.cs +++ b/tests/Ordering.UnitTests/Application/SetStockRejectedOrderStatusCommandTest.cs @@ -20,7 +20,7 @@ public void Set_Stock_Rejected_OrderStatusCommand_Check_Serialization() //Assert for List Assert.IsNotNull(deserializedCommand.OrderStockItems); - Assert.AreEqual(command.OrderStockItems.Count, deserializedCommand.OrderStockItems.Count); + Assert.HasCount(command.OrderStockItems.Count, deserializedCommand.OrderStockItems); for (int i = 0; i < command.OrderStockItems.Count; i++) { diff --git a/tests/Ordering.UnitTests/Domain/BuyerAggregateTest.cs b/tests/Ordering.UnitTests/Domain/BuyerAggregateTest.cs index 56a4835e8..58af6b514 100644 --- a/tests/Ordering.UnitTests/Domain/BuyerAggregateTest.cs +++ b/tests/Ordering.UnitTests/Domain/BuyerAggregateTest.cs @@ -125,6 +125,6 @@ public void Add_new_PaymentMethod_raises_new_event() fakeBuyer.VerifyOrAddPaymentMethod(cardTypeId, alias, cardNumber, cardSecurityNumber, cardHolderName, cardExpiration, orderId); //Assert - Assert.AreEqual(fakeBuyer.DomainEvents.Count, expectedResult); + Assert.HasCount(expectedResult, fakeBuyer.DomainEvents); } } diff --git a/tests/Ordering.UnitTests/Domain/OrderAggregateTest.cs b/tests/Ordering.UnitTests/Domain/OrderAggregateTest.cs index 7cbfc2bed..f0af353a1 100644 --- a/tests/Ordering.UnitTests/Domain/OrderAggregateTest.cs +++ b/tests/Ordering.UnitTests/Domain/OrderAggregateTest.cs @@ -125,7 +125,7 @@ public void Add_new_Order_raises_new_event() var fakeOrder = new Order("1", "fakeName", new Address(street, city, state, country, zipcode), cardTypeId, cardNumber, cardSecurityNumber, cardHolderName, cardExpiration); //Assert - Assert.AreEqual(fakeOrder.DomainEvents.Count, expectedResult); + Assert.HasCount(expectedResult, fakeOrder.DomainEvents); } [TestMethod] @@ -148,7 +148,7 @@ public void Add_event_Order_explicitly_raises_new_event() var fakeOrder = new Order("1", "fakeName", new Address(street, city, state, country, zipcode), cardTypeId, cardNumber, cardSecurityNumber, cardHolderName, cardExpiration); fakeOrder.AddDomainEvent(new OrderStartedDomainEvent(fakeOrder, "fakeName", "1", cardTypeId, cardNumber, cardSecurityNumber, cardHolderName, cardExpiration)); //Assert - Assert.AreEqual(fakeOrder.DomainEvents.Count, expectedResult); + Assert.HasCount(expectedResult, fakeOrder.DomainEvents); } [TestMethod] @@ -173,6 +173,6 @@ public void Remove_event_Order_explicitly() fakeOrder.AddDomainEvent(@fakeEvent); fakeOrder.RemoveDomainEvent(@fakeEvent); //Assert - Assert.AreEqual(fakeOrder.DomainEvents.Count, expectedResult); + Assert.HasCount(expectedResult, fakeOrder.DomainEvents); } } diff --git a/tests/Ordering.UnitTests/Domain/SeedWork/ValueObjectTests.cs b/tests/Ordering.UnitTests/Domain/SeedWork/ValueObjectTests.cs index f8575a3ae..f3acfd5b6 100644 --- a/tests/Ordering.UnitTests/Domain/SeedWork/ValueObjectTests.cs +++ b/tests/Ordering.UnitTests/Domain/SeedWork/ValueObjectTests.cs @@ -1,10 +1,8 @@ namespace eShop.Ordering.UnitTests.Domain.SeedWork; +[TestClass] public class ValueObjectTests { - public ValueObjectTests() - { } - [TestMethod] [DynamicData(nameof(EqualValueObjects))] public void Equals_EqualValueObjects_ReturnsTrue(ValueObject instanceA, ValueObject instanceB, string reason) diff --git a/tests/Ordering.UnitTests/GlobalUsings.cs b/tests/Ordering.UnitTests/GlobalUsings.cs index 645c7e329..a76b01211 100644 --- a/tests/Ordering.UnitTests/GlobalUsings.cs +++ b/tests/Ordering.UnitTests/GlobalUsings.cs @@ -1,4 +1,9 @@ -global using MediatR; +global using System; +global using System.Collections.Generic; +global using System.Linq; +global using System.Threading; +global using System.Threading.Tasks; +global using MediatR; global using Microsoft.AspNetCore.Mvc; global using eShop.Ordering.API.Application.Commands; global using eShop.Ordering.API.Application.Models; @@ -12,3 +17,5 @@ global using NSubstitute; global using eShop.Ordering.UnitTests; global using Microsoft.VisualStudio.TestTools.UnitTesting; + +[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)] diff --git a/tests/Ordering.UnitTests/Ordering.UnitTests.csproj b/tests/Ordering.UnitTests/Ordering.UnitTests.csproj index ec9494e22..56a9da555 100644 --- a/tests/Ordering.UnitTests/Ordering.UnitTests.csproj +++ b/tests/Ordering.UnitTests/Ordering.UnitTests.csproj @@ -1,16 +1,14 @@ - + net10.0 false false false + Exe - - - all