From 7539d738d51b3ce7a0c755388fa6166134763433 Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Thu, 28 Mar 2024 20:35:48 +0100 Subject: [PATCH] refactor: Simplify more tests --- .../TransformBlogPostRecordsJobTests.cs | 16 ++-- .../Shared/Services/SitemapServiceTests.cs | 74 +++++++++---------- 2 files changed, 43 insertions(+), 47 deletions(-) diff --git a/tests/LinkDotNet.Blog.IntegrationTests/Web/Features/TransformBlogPostRecordsJobTests.cs b/tests/LinkDotNet.Blog.IntegrationTests/Web/Features/TransformBlogPostRecordsJobTests.cs index 5edad253..35ab2250 100644 --- a/tests/LinkDotNet.Blog.IntegrationTests/Web/Features/TransformBlogPostRecordsJobTests.cs +++ b/tests/LinkDotNet.Blog.IntegrationTests/Web/Features/TransformBlogPostRecordsJobTests.cs @@ -15,8 +15,8 @@ namespace LinkDotNet.Blog.IntegrationTests.Web.Features; public class TransformBlogPostRecordsJobTests : SqlDatabaseTestBase { private readonly TransformBlogPostRecordsJob sut; - private readonly IRepository blogPostRecordRepository; - private readonly IRepository userRecordRepository; + private readonly Repository blogPostRecordRepository; + private readonly Repository userRecordRepository; public TransformBlogPostRecordsJobTests() { @@ -37,23 +37,23 @@ public async Task ShouldTransformRecords() { // Arrange var someDate = new DateOnly(2023, 08, 13); - var blogPosts = new List - { + List blogPosts = + [ new BlogPostBuilder().Build(), new BlogPostBuilder().Build(), new BlogPostBuilder().Build(), new BlogPostBuilder().Build(), - }; + ]; await Repository.StoreBulkAsync(blogPosts); - var userRecords = new List - { + List userRecords = + [ new() { Id = "A", DateClicked = someDate, UrlClicked = $"blogPost/{blogPosts[0].Id}" }, new() { Id = "B", DateClicked = someDate, UrlClicked = $"blogPost/{blogPosts[0].Id}/suffix" }, new() { Id = "C", DateClicked = someDate.AddDays(-3), UrlClicked = $"blogPost/{blogPosts[1].Id}" }, new() { Id = "D", DateClicked = someDate.AddDays(-3), UrlClicked = $"blogPost/{blogPosts[1].Id}" }, new() { Id = "E", DateClicked = someDate.AddDays(-2), UrlClicked = $"blogPost/{blogPosts[2].Id}" } - }; + ]; await userRecordRepository.StoreBulkAsync(userRecords); // Act diff --git a/tests/LinkDotNet.Blog.IntegrationTests/Web/Shared/Services/SitemapServiceTests.cs b/tests/LinkDotNet.Blog.IntegrationTests/Web/Shared/Services/SitemapServiceTests.cs index 358c5ede..5bd08291 100644 --- a/tests/LinkDotNet.Blog.IntegrationTests/Web/Shared/Services/SitemapServiceTests.cs +++ b/tests/LinkDotNet.Blog.IntegrationTests/Web/Shared/Services/SitemapServiceTests.cs @@ -6,57 +6,53 @@ using LinkDotNet.Blog.Infrastructure.Persistence; using LinkDotNet.Blog.Web.Features.Admin.Sitemap.Services; -namespace LinkDotNet.Blog.IntegrationTests.Web.Shared.Services +namespace LinkDotNet.Blog.IntegrationTests.Web.Shared.Services; + +public sealed class SitemapServiceTests : IDisposable { - public sealed class SitemapServiceTests : IDisposable - { - private const string OutputDirectory = "wwwroot"; - private const string OutputFilename = $"{OutputDirectory}/sitemap.xml"; - private readonly SitemapService sut; + private const string OutputDirectory = "wwwroot"; + private const string OutputFilename = $"{OutputDirectory}/sitemap.xml"; + private readonly SitemapService sut; - public SitemapServiceTests() - { - var repositoryMock = Substitute.For>(); - sut = new SitemapService(repositoryMock, null, new XmlFileWriter()); - Directory.CreateDirectory("wwwroot"); - } + public SitemapServiceTests() + { + var repositoryMock = Substitute.For>(); + sut = new SitemapService(repositoryMock, null, new XmlFileWriter()); + Directory.CreateDirectory("wwwroot"); + } - [Fact] - public async Task ShouldSaveSitemapUrlInCorrectFormat() + [Fact] + public async Task ShouldSaveSitemapUrlInCorrectFormat() + { + var urlSet = new SitemapUrlSet { - var urlSet = new SitemapUrlSet() - { - Urls = new List - { - new SitemapUrl - { - Location = "here", - }, - }, - }; - await sut.SaveSitemapToFileAsync(urlSet); + Urls = + [ + new SitemapUrl { Location = "here", } + ], + }; + await sut.SaveSitemapToFileAsync(urlSet); - var lines = await File.ReadAllTextAsync(OutputFilename); - lines.Should().Be( -@" + var lines = await File.ReadAllTextAsync(OutputFilename); + lines.Should().Be( + @" here "); - } + } - public void Dispose() + public void Dispose() + { + if (File.Exists(OutputFilename)) { - if (File.Exists(OutputFilename)) - { - File.Delete(OutputFilename); - } + File.Delete(OutputFilename); + } - if (Directory.Exists(OutputDirectory)) - { - Directory.Delete(OutputDirectory, true); - } + if (Directory.Exists(OutputDirectory)) + { + Directory.Delete(OutputDirectory, true); } } -} +} \ No newline at end of file