Skip to content

Commit

Permalink
Migrate to xunit.v3
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Sep 24, 2024
1 parent 1d3f361 commit 86bf9f8
Show file tree
Hide file tree
Showing 30 changed files with 87 additions and 70 deletions.
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<PackageVersion Include="bunit.generators" Version="2.0.24-preview" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
<PackageVersion Include="xunit" Version="2.9.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="xunit.v3" Version="0.4.0-pre.20" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.0-pre.35" />
</ItemGroup>
</Project>
</Project>
20 changes: 13 additions & 7 deletions tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
<NuGetAuditLevel>critical</NuGetAuditLevel>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Bunit</InterceptorsPreviewNamespaces>
</PropertyGroup>

<ItemGroup Condition="$(MSBuildProjectName) != 'LinkDotNet.Blog.TestUtilities'">
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit.v3" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="bunit.generators" />
<PackageReference Include="bunit" />
<PackageReference Include="Shouldly" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand All @@ -26,11 +29,14 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="$(MSBuildProjectName) != 'LinkDotNet.Blog.TestUtilities'">
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup Label="Implicit usings">
<Using Include="Shouldly" />
<Using Include="NSubstitute" />
<Using Include="Xunit" />
<Using Include="Bunit" />
<Using Include="Bunit.TestDoubles" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Raven.Client.Documents;
using Raven.Embedded;
using Raven.TestDriver;
using TestContext = Xunit.TestContext;

namespace LinkDotNet.Blog.IntegrationTests.Infrastructure.Persistence.RavenDb;

Expand Down Expand Up @@ -149,7 +150,7 @@ public async Task ShouldDelete()
await sut.DeleteAsync(blogPost.Id);

using var session = store.OpenAsyncSession();
(await session.Query<BlogPost>().AnyAsync(b => b.Id == blogPost.Id)).ShouldBeFalse();
(await session.Query<BlogPost>().AnyAsync(b => b.Id == blogPost.Id, TestContext.Current.CancellationToken)).ShouldBeFalse();
}

public override void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using LinkDotNet.Blog.TestUtilities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using TestContext = Xunit.TestContext;

namespace LinkDotNet.Blog.IntegrationTests.Infrastructure.Persistence.Sql;

Expand All @@ -14,8 +15,8 @@ public sealed class BlogPostRepositoryTests : SqlDatabaseTestBase<BlogPost>
public async Task ShouldLoadBlogPost()
{
var blogPost = BlogPost.Create("Title", "Subtitle", "Content", "url", true, tags: new[] { "Tag 1", "Tag 2" });
await DbContext.BlogPosts.AddAsync(blogPost);
await DbContext.SaveChangesAsync();
await DbContext.BlogPosts.AddAsync(blogPost, TestContext.Current.CancellationToken);
await DbContext.SaveChangesAsync(TestContext.Current.CancellationToken);

var blogPostFromRepo = await Repository.GetByIdAsync(blogPost.Id);

Expand All @@ -38,7 +39,10 @@ public async Task ShouldSaveBlogPost()

await Repository.StoreAsync(blogPost);

var blogPostFromContext = await DbContext.BlogPosts.AsNoTracking().SingleOrDefaultAsync(s => s.Id == blogPost.Id);
var blogPostFromContext = await DbContext
.BlogPosts
.AsNoTracking()
.SingleOrDefaultAsync(s => s.Id == blogPost.Id, TestContext.Current.CancellationToken);
blogPostFromContext.ShouldNotBeNull();
blogPostFromContext.Title.ShouldBe("Title");
blogPostFromContext.ShortDescription.ShouldBe("Subtitle");
Expand All @@ -55,8 +59,8 @@ public async Task ShouldSaveBlogPost()
public async Task ShouldGetAllBlogPosts()
{
var blogPost = BlogPost.Create("Title", "Subtitle", "Content", "url", true, tags: new[] { "Tag 1", "Tag 2" });
await DbContext.BlogPosts.AddAsync(blogPost);
await DbContext.SaveChangesAsync();
await DbContext.BlogPosts.AddAsync(blogPost, TestContext.Current.CancellationToken);
await DbContext.SaveChangesAsync(TestContext.Current.CancellationToken);

var blogPostsFromRepo = await Repository.GetAllAsync();

Expand All @@ -78,15 +82,15 @@ public async Task ShouldGetAllBlogPosts()
public async Task ShouldBeUpdateable()
{
var blogPost = new BlogPostBuilder().Build();
await DbContext.BlogPosts.AddAsync(blogPost);
await DbContext.SaveChangesAsync();
await DbContext.BlogPosts.AddAsync(blogPost, TestContext.Current.CancellationToken);
await DbContext.SaveChangesAsync(TestContext.Current.CancellationToken);
var blogPostFromDb = await Repository.GetByIdAsync(blogPost.Id);
var updater = new BlogPostBuilder().WithTitle("New Title").Build();
blogPostFromDb!.Update(updater);

await Repository.StoreAsync(blogPostFromDb);

var blogPostAfterSave = await DbContext.BlogPosts.AsNoTracking().SingleAsync(b => b.Id == blogPostFromDb.Id);
var blogPostAfterSave = await DbContext.BlogPosts.AsNoTracking().SingleAsync(b => b.Id == blogPostFromDb.Id, TestContext.Current.CancellationToken);
blogPostAfterSave.Title.ShouldBe("New Title");
}

Expand Down Expand Up @@ -119,7 +123,7 @@ public async Task ShouldDelete()

await Repository.DeleteAsync(blogPost.Id);

(await DbContext.BlogPosts.AsNoTracking().AnyAsync(b => b.Id == blogPost.Id)).ShouldBeFalse();
(await DbContext.BlogPosts.AsNoTracking().AnyAsync(b => b.Id == blogPost.Id, TestContext.Current.CancellationToken)).ShouldBeFalse();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
13 changes: 7 additions & 6 deletions tests/LinkDotNet.Blog.IntegrationTests/SmokeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using LinkDotNet.Blog.Infrastructure.Persistence;
using LinkDotNet.Blog.Web;
using Microsoft.AspNetCore.Mvc.Testing;
using TestContext = Xunit.TestContext;

namespace LinkDotNet.Blog.IntegrationTests;

Expand All @@ -23,7 +24,7 @@ public async Task ShouldBootUpApplication()
{
using var client = factory.CreateClient();

var result = await client.GetAsync("/");
var result = await client.GetAsync("/", cancellationToken: TestContext.Current.CancellationToken);

result.IsSuccessStatusCode.ShouldBeTrue();
}
Expand All @@ -38,7 +39,7 @@ public async Task ShouldBootUpWithSqlDatabase()
});
using var client = sqlFactory.CreateClient();

var result = await client.GetAsync("/");
var result = await client.GetAsync("/", cancellationToken: TestContext.Current.CancellationToken);

result.IsSuccessStatusCode.ShouldBeTrue();
}
Expand All @@ -48,7 +49,7 @@ public async Task ShouldAllowDotsForTagSearch()
{
using var client = factory.CreateClient();

var result = await client.GetAsync("/searchByTag/.NET5");
var result = await client.GetAsync("/searchByTag/.NET5", cancellationToken: TestContext.Current.CancellationToken);

result.IsSuccessStatusCode.ShouldBeTrue();
}
Expand All @@ -58,7 +59,7 @@ public async Task ShouldAllowDotsForFreeTextSearch()
{
using var client = factory.CreateClient();

var result = await client.GetAsync("/search/.NET5");
var result = await client.GetAsync("/search/.NET5", cancellationToken: TestContext.Current.CancellationToken);

result.IsSuccessStatusCode.ShouldBeTrue();
}
Expand All @@ -71,11 +72,11 @@ public async Task RssFeedShouldBeRateLimited()

for (var i = 0; i < numberOfRequests - 1; i++)
{
var result = await client.GetAsync("/feed.rss");
var result = await client.GetAsync("/feed.rss", cancellationToken: TestContext.Current.CancellationToken);
result.IsSuccessStatusCode.ShouldBeTrue();
}

var lastResult = await client.GetAsync("/feed.rss");
var lastResult = await client.GetAsync("/feed.rss", cancellationToken: TestContext.Current.CancellationToken);
lastResult.IsSuccessStatusCode.ShouldBeFalse();
}

Expand Down
14 changes: 2 additions & 12 deletions tests/LinkDotNet.Blog.IntegrationTests/SqlDatabaseTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace LinkDotNet.Blog.IntegrationTests;

public abstract class SqlDatabaseTestBase<TEntity> : IAsyncLifetime, IAsyncDisposable
public abstract class SqlDatabaseTestBase<TEntity> : IAsyncDisposable
where TEntity : Entity
{
protected SqlDatabaseTestBase()
Expand All @@ -30,17 +30,7 @@ protected SqlDatabaseTestBase()
protected BlogDbContext DbContext { get; }

protected IDbContextFactory<BlogDbContext> DbContextFactory { get; }

public Task InitializeAsync()
{
return Task.CompletedTask;
}

async Task IAsyncLifetime.DisposeAsync()
{
await DisposeAsync();
}


public async ValueTask DisposeAsync()
{
GC.SuppressFinalize(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using LinkDotNet.Blog.Web.Features.Components;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using TestContext = Xunit.TestContext;

namespace LinkDotNet.Blog.IntegrationTests.Web.Features.AboutMe.Components;

Expand Down Expand Up @@ -61,7 +62,7 @@ public async Task WhenUserClickDeleteButtonThenDeleted()

cut.WaitForState(() => !cut.HasComponent<TalkEntry>());
cut.HasComponent<TalkEntry>().ShouldBeFalse();
(await DbContext.Talks.AnyAsync()).ShouldBeFalse();
(await DbContext.Talks.AnyAsync(cancellationToken: TestContext.Current.CancellationToken)).ShouldBeFalse();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using NCronJob;
using TestContext = Xunit.TestContext;

namespace LinkDotNet.Blog.IntegrationTests.Web.Features.Admin.BlogPostEditor;

Expand All @@ -36,7 +37,7 @@ public async Task ShouldSaveBlogPostOnSave()

TriggerNewBlogPost(newBlogPost);

var blogPostFromDb = await DbContext.BlogPosts.SingleOrDefaultAsync(t => t.Title == "My Title");
var blogPostFromDb = await DbContext.BlogPosts.SingleOrDefaultAsync(t => t.Title == "My Title", TestContext.Current.CancellationToken);
blogPostFromDb.ShouldNotBeNull();
blogPostFromDb.ShortDescription.ShouldBe("My short Description");
toastService.Received(1).ShowInfo("Created BlogPost My Title", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using NCronJob;
using TestContext = Xunit.TestContext;

namespace LinkDotNet.Blog.IntegrationTests.Web.Features.Admin.BlogPostEditor;

Expand All @@ -40,7 +41,7 @@ public async Task ShouldSaveBlogPostOnSave()

TriggerUpdate(newBlogPost);

var blogPostFromDb = await DbContext.BlogPosts.SingleOrDefaultAsync(t => t.Id == blogPost.Id);
var blogPostFromDb = await DbContext.BlogPosts.SingleOrDefaultAsync(t => t.Id == blogPost.Id, TestContext.Current.CancellationToken);
blogPostFromDb.ShouldNotBeNull();
blogPostFromDb.ShortDescription.ShouldBe("My new Description");
toastService.Received(1).ShowInfo("Updated BlogPost Title", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using LinkDotNet.Blog.Web.Features.Admin.Dashboard.Components;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using TestContext = Xunit.TestContext;

namespace LinkDotNet.Blog.IntegrationTests.Web.Features.Admin.Dashboard.Components;

Expand Down Expand Up @@ -51,7 +52,7 @@ public async Task ShouldFilterByDate()
var clicked4 = new BlogPostRecord
{ BlogPostId = blogPost1.Id, DateClicked = new DateOnly(2021, 1, 1), Clicks = 1 };
await DbContext.BlogPostRecords.AddRangeAsync(clicked1, clicked2, clicked3, clicked4);
await DbContext.SaveChangesAsync();
await DbContext.SaveChangesAsync(TestContext.Current.CancellationToken);
await using var ctx = new BunitContext();
ctx.ComponentFactories.AddStub<DateRangeSelector>();
RegisterRepositories(ctx);
Expand Down Expand Up @@ -83,7 +84,7 @@ public async Task ShouldShowTotalClickCount()
var clicked3 = new BlogPostRecord
{ BlogPostId = blogPost2.Id, DateClicked = DateOnly.MinValue, Clicks = 1 };
await DbContext.BlogPostRecords.AddRangeAsync(clicked1, clicked2, clicked3);
await DbContext.SaveChangesAsync();
await DbContext.SaveChangesAsync(TestContext.Current.CancellationToken);
await using var ctx = new BunitContext();
RegisterRepositories(ctx);

Expand All @@ -105,7 +106,7 @@ public async Task GivenDeletedBlogPostWhenEncounteringBlogPostRecordThenNoError(
var clicked3 = new BlogPostRecord
{ BlogPostId = Guid.NewGuid().ToString(), DateClicked = DateOnly.MinValue, Clicks = 1 };
await DbContext.BlogPostRecords.AddRangeAsync(clicked1, clicked2, clicked3);
await DbContext.SaveChangesAsync();
await DbContext.SaveChangesAsync(TestContext.Current.CancellationToken);
await using var ctx = new BunitContext();
RegisterRepositories(ctx);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void ShouldSetAbsoluteUriForOgData(string givenUri, string expectedUri)
}

[Theory]
[InlineData(null)]
[InlineData(null!)]
[InlineData(0)]
[InlineData(-1)]
public async Task ShouldSetPageToFirstIfOutOfRange(int? page)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using LinkDotNet.Blog.Web.Features.ShowBlogPost.Components;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using TestContext = Xunit.TestContext;

namespace LinkDotNet.Blog.IntegrationTests.Web.Features.ShowBlogPost.Components;

Expand All @@ -26,8 +27,8 @@ public async Task ShouldShowSimilarBlogPosts()
Id = blogPost1.Id,
SimilarBlogPostIds = [blogPost2.Id, blogPost3.Id]
};
await DbContext.SimilarBlogPosts.AddAsync(similarBlogPost1);
await DbContext.SaveChangesAsync();
await DbContext.SimilarBlogPosts.AddAsync(similarBlogPost1, TestContext.Current.CancellationToken);
await DbContext.SaveChangesAsync(TestContext.Current.CancellationToken);
await using var context = new BunitContext();
context.Services.AddScoped<IRepository<SimilarBlogPost>>(_ =>
new Repository<SimilarBlogPost>(DbContextFactory, Substitute.For<ILogger<Repository<SimilarBlogPost>>>()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NCronJob;
using TestContext = Xunit.TestContext;

namespace LinkDotNet.Blog.IntegrationTests.Web.Features.ShowBlogPost;

Expand All @@ -33,7 +34,7 @@ public async Task ShouldAddLikeOnEvent()

likeComponent.Find("span").Click();

var fromDb = await DbContext.BlogPosts.AsNoTracking().SingleAsync(d => d.Id == publishedPost.Id);
var fromDb = await DbContext.BlogPosts.AsNoTracking().SingleAsync(d => d.Id == publishedPost.Id, TestContext.Current.CancellationToken);
fromDb.Likes.ShouldBe(3);
}

Expand All @@ -57,7 +58,7 @@ public async Task ShouldSubtractLikeOnEvent()

likeComponent.Find("span").Click();

var fromDb = await DbContext.BlogPosts.AsNoTracking().SingleAsync(d => d.Id == publishedPost.Id);
var fromDb = await DbContext.BlogPosts.AsNoTracking().SingleAsync(d => d.Id == publishedPost.Id, TestContext.Current.CancellationToken);
fromDb.Likes.ShouldBe(1);
}

Expand Down
Loading

0 comments on commit 86bf9f8

Please sign in to comment.