Skip to content

Commit

Permalink
refactor: Use Shouldly
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Sep 6, 2024
1 parent 90eef14 commit 914e492
Show file tree
Hide file tree
Showing 81 changed files with 578 additions and 568 deletions.
4 changes: 2 additions & 2 deletions tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="bunit.generators" Version="2.0.24-preview" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="bunit" Version="2.0.24-preview" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="xunit" Version="2.9.0" />
Expand All @@ -35,7 +35,7 @@
</ItemGroup>

<ItemGroup Label="Implicit usings">
<Using Include="FluentAssertions" />
<Using Include="Shouldly" />
<Using Include="NSubstitute" />
<Using Include="Xunit" />
<Using Include="Bunit" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public async Task ShouldNotCacheWhenDifferentQueries()

var allWithTag2 = await sut.GetAllAsync(f => f.Tags.Any(t => t == searchTerm));

allWithTag2.Count.Should().Be(1);
allWithTag2.Single().Tags.Single().Should().Be("tag 2");
allWithTag2.Count.ShouldBe(1);
allWithTag2.Single().Tags.Single().ShouldBe("tag 2");
}

[Fact]
Expand All @@ -42,7 +42,7 @@ public async Task ShouldResetOnDelete()

var db = await sut.GetAllAsync();

db.Single().Title.Should().Be("2");
db.Single().Title.ShouldBe("2");
}

[Fact]
Expand All @@ -58,6 +58,6 @@ public async Task ShouldResetOnSave()

var db = await sut.GetAllAsync();

db.Single().Title.Should().Be("2");
db.Single().Title.ShouldBe("2");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ public async Task ShouldLoadBlogPost()

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

blogPostFromRepo.Should().NotBeNull();
blogPostFromRepo.Title.Should().Be("Title");
blogPostFromRepo.ShortDescription.Should().Be("Subtitle");
blogPostFromRepo.Content.Should().Be("Content");
blogPostFromRepo.PreviewImageUrl.Should().Be("url");
blogPostFromRepo.IsPublished.Should().BeTrue();
blogPostFromRepo.Tags.Should().HaveCount(2);
blogPostFromRepo.ShouldNotBeNull();
blogPostFromRepo.Title.ShouldBe("Title");
blogPostFromRepo.ShortDescription.ShouldBe("Subtitle");
blogPostFromRepo.Content.ShouldBe("Content");
blogPostFromRepo.PreviewImageUrl.ShouldBe("url");
blogPostFromRepo.IsPublished.ShouldBeTrue();
blogPostFromRepo.Tags.Count.ShouldBe(2);
var tagContent = blogPostFromRepo.Tags;
tagContent.Should().Contain(new[] { "Tag 1", "Tag 2" });
tagContent.ShouldContain("Tag 1");
tagContent.ShouldContain("Tag 2");
}

[Fact]
Expand All @@ -58,9 +59,9 @@ public async Task ShouldFilterAndOrder()
false);

var retrievedPosts = blogPosts.ToList();
retrievedPosts.Exists(b => b.Id == filteredOutPost.Id).Should().BeFalse();
retrievedPosts[0].Id.Should().Be(olderPost.Id);
retrievedPosts[1].Id.Should().Be(newerPost.Id);
retrievedPosts.Exists(b => b.Id == filteredOutPost.Id).ShouldBeFalse();
retrievedPosts[0].Id.ShouldBe(olderPost.Id);
retrievedPosts[1].Id.ShouldBe(newerPost.Id);
}

[Fact]
Expand All @@ -77,8 +78,8 @@ public async Task ShouldSort()
descending: true);

var retrievedPosts = blogPosts.ToList();
retrievedPosts[0].Id.Should().Be(newerPost.Id);
retrievedPosts[1].Id.Should().Be(olderPost.Id);
retrievedPosts[0].Id.ShouldBe(newerPost.Id);
retrievedPosts[1].Id.ShouldBe(olderPost.Id);
}

[Fact]
Expand All @@ -89,15 +90,16 @@ public async Task ShouldSaveBlogPost()
await sut.StoreAsync(blogPost);

var blogPostFromContext = await GetBlogPostByIdAsync(blogPost.Id);
blogPostFromContext.Should().NotBeNull();
blogPostFromContext.Title.Should().Be("Title");
blogPostFromContext.ShortDescription.Should().Be("Subtitle");
blogPostFromContext.Content.Should().Be("Content");
blogPostFromContext.IsPublished.Should().BeTrue();
blogPostFromContext.PreviewImageUrl.Should().Be("url");
blogPostFromContext.Tags.Should().HaveCount(2);
blogPostFromContext.ShouldNotBeNull();
blogPostFromContext.Title.ShouldBe("Title");
blogPostFromContext.ShortDescription.ShouldBe("Subtitle");
blogPostFromContext.Content.ShouldBe("Content");
blogPostFromContext.IsPublished.ShouldBeTrue();
blogPostFromContext.PreviewImageUrl.ShouldBe("url");
blogPostFromContext.Tags.Count.ShouldBe(2);
var tagContent = blogPostFromContext.Tags;
tagContent.Should().Contain(new[] { "Tag 1", "Tag 2" });
tagContent.ShouldContain("Tag 1");
tagContent.ShouldContain("Tag 2");
}

[Fact]
Expand All @@ -108,17 +110,18 @@ public async Task ShouldGetAllBlogPosts()

var blogPostsFromRepo = await sut.GetAllAsync();

blogPostsFromRepo.Should().NotBeNull();
blogPostsFromRepo.Should().HaveCount(1);
blogPostsFromRepo.ShouldNotBeNull();
blogPostsFromRepo.ShouldHaveSingleItem();
var blogPostFromRepo = blogPostsFromRepo.Single();
blogPostFromRepo.Title.Should().Be("Title");
blogPostFromRepo.ShortDescription.Should().Be("Subtitle");
blogPostFromRepo.Content.Should().Be("Content");
blogPostFromRepo.PreviewImageUrl.Should().Be("url");
blogPostFromRepo.IsPublished.Should().BeTrue();
blogPostFromRepo.Tags.Should().HaveCount(2);
blogPostFromRepo.Title.ShouldBe("Title");
blogPostFromRepo.ShortDescription.ShouldBe("Subtitle");
blogPostFromRepo.Content.ShouldBe("Content");
blogPostFromRepo.PreviewImageUrl.ShouldBe("url");
blogPostFromRepo.IsPublished.ShouldBeTrue();
blogPostFromRepo.Tags.Count.ShouldBe(2);
var tagContent = blogPostFromRepo.Tags;
tagContent.Should().Contain(new[] { "Tag 1", "Tag 2" });
tagContent.ShouldContain("Tag 1");
tagContent.ShouldContain("Tag 2");
}

[Fact]
Expand All @@ -128,12 +131,12 @@ public async Task ShouldBeUpdateable()
await SaveBlogPostAsync(blogPost);
var blogPostFromDb = await sut.GetByIdAsync(blogPost.Id);
var updater = new BlogPostBuilder().WithTitle("New Title").Build();
blogPostFromDb.Update(updater);
blogPostFromDb!.Update(updater);

await sut.StoreAsync(blogPostFromDb);

var blogPostAfterSave = await GetBlogPostByIdAsync(blogPost.Id);
blogPostAfterSave.Title.Should().Be("New Title");
blogPostAfterSave.Title.ShouldBe("New Title");
}

[Fact]
Expand All @@ -145,7 +148,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)).Should().BeFalse();
(await session.Query<BlogPost>().AnyAsync(b => b.Id == blogPost.Id)).ShouldBeFalse();
}

public override void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ public async Task ShouldLoadBlogPost()

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

blogPostFromRepo.Should().NotBeNull();
blogPostFromRepo.Title.Should().Be("Title");
blogPostFromRepo.ShortDescription.Should().Be("Subtitle");
blogPostFromRepo.Content.Should().Be("Content");
blogPostFromRepo.PreviewImageUrl.Should().Be("url");
blogPostFromRepo.IsPublished.Should().BeTrue();
blogPostFromRepo.Tags.Should().HaveCount(2);
blogPostFromRepo.ShouldNotBeNull();
blogPostFromRepo.Title.ShouldBe("Title");
blogPostFromRepo.ShortDescription.ShouldBe("Subtitle");
blogPostFromRepo.Content.ShouldBe("Content");
blogPostFromRepo.PreviewImageUrl.ShouldBe("url");
blogPostFromRepo.IsPublished.ShouldBeTrue();
blogPostFromRepo.Tags.Count.ShouldBe(2);
var tagContent = blogPostFromRepo.Tags;
tagContent.Should().Contain(new[] { "Tag 1", "Tag 2" });
tagContent.ShouldContain("Tag 1");
tagContent.ShouldContain("Tag 2");
}

[Fact]
Expand All @@ -38,15 +39,16 @@ public async Task ShouldSaveBlogPost()
await Repository.StoreAsync(blogPost);

var blogPostFromContext = await DbContext.BlogPosts.AsNoTracking().SingleOrDefaultAsync(s => s.Id == blogPost.Id);
blogPostFromContext.Should().NotBeNull();
blogPostFromContext.Title.Should().Be("Title");
blogPostFromContext.ShortDescription.Should().Be("Subtitle");
blogPostFromContext.Content.Should().Be("Content");
blogPostFromContext.IsPublished.Should().BeTrue();
blogPostFromContext.PreviewImageUrl.Should().Be("url");
blogPostFromContext.Tags.Should().HaveCount(2);
blogPostFromContext.ShouldNotBeNull();
blogPostFromContext.Title.ShouldBe("Title");
blogPostFromContext.ShortDescription.ShouldBe("Subtitle");
blogPostFromContext.Content.ShouldBe("Content");
blogPostFromContext.IsPublished.ShouldBeTrue();
blogPostFromContext.PreviewImageUrl.ShouldBe("url");
blogPostFromContext.Tags.Count.ShouldBe(2);
var tagContent = blogPostFromContext.Tags;
tagContent.Should().Contain(new[] { "Tag 1", "Tag 2" });
tagContent.ShouldContain("Tag 1");
tagContent.ShouldContain("Tag 2");
}

[Fact]
Expand All @@ -58,17 +60,18 @@ public async Task ShouldGetAllBlogPosts()

var blogPostsFromRepo = await Repository.GetAllAsync();

blogPostsFromRepo.Should().NotBeNull();
blogPostsFromRepo.Should().HaveCount(1);
blogPostsFromRepo.ShouldNotBeNull();
blogPostsFromRepo.ShouldHaveSingleItem();
var blogPostFromRepo = blogPostsFromRepo.Single();
blogPostFromRepo.Title.Should().Be("Title");
blogPostFromRepo.ShortDescription.Should().Be("Subtitle");
blogPostFromRepo.Content.Should().Be("Content");
blogPostFromRepo.PreviewImageUrl.Should().Be("url");
blogPostFromRepo.IsPublished.Should().BeTrue();
blogPostFromRepo.Tags.Should().HaveCount(2);
blogPostFromRepo.Title.ShouldBe("Title");
blogPostFromRepo.ShortDescription.ShouldBe("Subtitle");
blogPostFromRepo.Content.ShouldBe("Content");
blogPostFromRepo.PreviewImageUrl.ShouldBe("url");
blogPostFromRepo.IsPublished.ShouldBeTrue();
blogPostFromRepo.Tags.Count.ShouldBe(2);
var tagContent = blogPostFromRepo.Tags;
tagContent.Should().Contain(new[] { "Tag 1", "Tag 2" });
tagContent.ShouldContain("Tag 1");
tagContent.ShouldContain("Tag 2");
}

[Fact]
Expand All @@ -84,7 +87,7 @@ public async Task ShouldBeUpdateable()
await Repository.StoreAsync(blogPostFromDb);

var blogPostAfterSave = await DbContext.BlogPosts.AsNoTracking().SingleAsync(b => b.Id == blogPostFromDb.Id);
blogPostAfterSave.Title.Should().Be("New Title");
blogPostAfterSave.Title.ShouldBe("New Title");
}

[Fact]
Expand All @@ -103,9 +106,9 @@ public async Task ShouldFilterAndOrder()
false);

var retrievedPosts = blogPosts.ToList();
retrievedPosts.Exists(b => b.Id == filteredOutPost.Id).Should().BeFalse();
retrievedPosts[0].Id.Should().Be(olderPost.Id);
retrievedPosts[1].Id.Should().Be(newerPost.Id);
retrievedPosts.Exists(b => b.Id == filteredOutPost.Id).ShouldBeFalse();
retrievedPosts[0].Id.ShouldBe(olderPost.Id);
retrievedPosts[1].Id.ShouldBe(newerPost.Id);
}

[Fact]
Expand All @@ -116,7 +119,7 @@ public async Task ShouldDelete()

await Repository.DeleteAsync(blogPost.Id);

(await DbContext.BlogPosts.AsNoTracking().AnyAsync(b => b.Id == blogPost.Id)).Should().BeFalse();
(await DbContext.BlogPosts.AsNoTracking().AnyAsync(b => b.Id == blogPost.Id)).ShouldBeFalse();
}

[Fact]
Expand All @@ -132,6 +135,6 @@ public async Task GivenBlogPostWithTags_WhenLoadingAndDeleting_ThenShouldBeUpdat

var bpFromDb = await sut.GetByIdAsync(bp.Id);

bpFromDb.Tags.Single().Should().Be("tag 2");
bpFromDb.Tags.Single().ShouldBe("tag 2");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public async Task ShouldSaveAndRetrieveAllEntries()

var items = await Repository.GetAllAsync();

items[0].Content.Should().Be("key1");
items[1].Content.Should().Be("key2");
items[0].Content.ShouldBe("key1");
items[1].Content.ShouldBe("key2");
}

[Fact]
Expand All @@ -31,8 +31,8 @@ public async Task ShouldDelete()
await Repository.DeleteAsync(item1.Id);

var items = await Repository.GetAllAsync();
items.Should().HaveCount(1);
items[0].Id.Should().Be(item2.Id);
items.ShouldHaveSingleItem();
items[0].Id.ShouldBe(item2.Id);
}

[Fact]
Expand All @@ -43,6 +43,6 @@ public async Task NoopOnDeleteWhenEntryNotFound()

await Repository.DeleteAsync("SomeIdWhichHopefullyDoesNotExist");

(await Repository.GetAllAsync()).Should().HaveCount(1);
(await Repository.GetAllAsync()).ShouldHaveSingleItem();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public async Task ShouldSaveAndRetrieveAllEntries()

var items = await Repository.GetAllAsync();

items.Should().HaveCount(1);
items[0].Name.Should().Be("C#");
items[0].IconUrl.Should().Be("url");
items[0].Capability.Should().Be("Backend");
items[0].ProficiencyLevel.Should().Be(ProficiencyLevel.Expert);
items.ShouldHaveSingleItem();
items[0].Name.ShouldBe("C#");
items[0].IconUrl.ShouldBe("url");
items[0].Capability.ShouldBe("Backend");
items[0].ProficiencyLevel.ShouldBe(ProficiencyLevel.Expert);
}

[Fact]
Expand All @@ -36,8 +36,8 @@ public async Task ShouldDelete()
await Repository.DeleteAsync(skill1.Id);

var items = await Repository.GetAllAsync();
items.Should().HaveCount(1);
items[0].Id.Should().Be(skill2.Id);
items.ShouldHaveSingleItem();
items[0].Id.ShouldBe(skill2.Id);
}

[Fact]
Expand All @@ -48,6 +48,6 @@ public async Task NoopOnDeleteWhenEntryNotFound()

await Repository.DeleteAsync("SomeIdWhichHopefullyDoesNotExist");

(await Repository.GetAllAsync()).Should().HaveCount(1);
(await Repository.GetAllAsync()).ShouldHaveSingleItem();
}
}
8 changes: 4 additions & 4 deletions tests/LinkDotNet.Blog.IntegrationTests/SmokeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task ShouldBootUpApplication()

var result = await client.GetAsync("/");

result.IsSuccessStatusCode.Should().BeTrue();
result.IsSuccessStatusCode.ShouldBeTrue();
}

[Fact]
Expand All @@ -40,7 +40,7 @@ public async Task ShouldBootUpWithSqlDatabase()

var result = await client.GetAsync("/");

result.IsSuccessStatusCode.Should().BeTrue();
result.IsSuccessStatusCode.ShouldBeTrue();
}

[Fact]
Expand All @@ -50,7 +50,7 @@ public async Task ShouldAllowDotsForTagSearch()

var result = await client.GetAsync("/searchByTag/.NET5");

result.IsSuccessStatusCode.Should().BeTrue();
result.IsSuccessStatusCode.ShouldBeTrue();
}

[Fact]
Expand All @@ -60,7 +60,7 @@ public async Task ShouldAllowDotsForFreeTextSearch()

var result = await client.GetAsync("/search/.NET5");

result.IsSuccessStatusCode.Should().BeTrue();
result.IsSuccessStatusCode.ShouldBeTrue();
}

public void Dispose() => factory?.Dispose();
Expand Down
Loading

0 comments on commit 914e492

Please sign in to comment.