Skip to content

Commit 884958f

Browse files
committed
Replaced new Mock<T>.Object with Mock.Of<T>
1 parent 2b33298 commit 884958f

File tree

13 files changed

+36
-36
lines changed

13 files changed

+36
-36
lines changed

tests/LinkDotNet.Blog.IntegrationTests/Web/Pages/Admin/CreateNewBlogPostPageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task ShouldSaveBlogPostOnSave()
2727
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
2828
ctx.Services.AddScoped(_ => toastService.Object);
2929
ctx.ComponentFactories.AddStub<UploadFile>();
30-
ctx.Services.AddScoped(_ => new Mock<IFileProcessor>().Object);
30+
ctx.Services.AddScoped(_ => Mock.Of<IFileProcessor>());
3131
using var cut = ctx.RenderComponent<CreateNewBlogPostPage>();
3232
var newBlogPost = cut.FindComponent<CreateNewBlogPost>();
3333

tests/LinkDotNet.Blog.IntegrationTests/Web/Pages/Admin/UpdateBlogPostPageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void ShouldThrowWhenNoIdProvided()
4848
using var ctx = new TestContext();
4949
ctx.AddTestAuthorization().SetAuthorized("some username");
5050
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
51-
ctx.Services.AddScoped(_ => new Mock<IToastService>().Object);
51+
ctx.Services.AddScoped(_ => Mock.Of<IToastService>());
5252

5353
Action act = () => ctx.RenderComponent<UpdateBlogPostPage>(
5454
p => p.Add(s => s.BlogPostId, null));

tests/LinkDotNet.Blog.IntegrationTests/Web/Pages/BlogPostPageTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public async Task ShouldSetTagsWhenAvailable()
8383
private void RegisterComponents(TestContextBase ctx, ILocalStorageService localStorageService = null)
8484
{
8585
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
86-
ctx.Services.AddScoped(_ => localStorageService ?? new Mock<ILocalStorageService>().Object);
87-
ctx.Services.AddScoped(_ => new Mock<IToastService>().Object);
88-
ctx.Services.AddScoped(_ => new Mock<IUserRecordService>().Object);
86+
ctx.Services.AddScoped(_ => localStorageService ?? Mock.Of<ILocalStorageService>());
87+
ctx.Services.AddScoped(_ => Mock.Of<IToastService>());
88+
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
8989
ctx.Services.AddScoped(_ => new AppConfiguration());
9090
}
91-
}
91+
}

tests/LinkDotNet.Blog.IntegrationTests/Web/Pages/IndexTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,6 @@ private void RegisterComponents(TestContextBase ctx)
173173
{
174174
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
175175
ctx.Services.AddScoped(_ => CreateSampleAppConfiguration());
176-
ctx.Services.AddScoped(_ => new Mock<IUserRecordService>().Object);
176+
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
177177
}
178-
}
178+
}

tests/LinkDotNet.Blog.IntegrationTests/Web/Pages/SearchByTagTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public async Task ShouldOnlyDisplayTagsGivenByParameter()
2424
await AddBlogPostWithTagAsync("Tag 1");
2525
await AddBlogPostWithTagAsync("Tag 2");
2626
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
27-
ctx.Services.AddScoped(_ => new Mock<IUserRecordService>().Object);
27+
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
2828
var cut = ctx.RenderComponent<SearchByTag>(p => p.Add(s => s.Tag, "Tag 1"));
2929
cut.WaitForState(() => cut.FindAll(".blog-card").Any());
3030

@@ -39,7 +39,7 @@ public async Task ShouldHandleSpecialCharacters()
3939
using var ctx = new TestContext();
4040
await AddBlogPostWithTagAsync("C#");
4141
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
42-
ctx.Services.AddScoped(_ => new Mock<IUserRecordService>().Object);
42+
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
4343
var cut = ctx.RenderComponent<SearchByTag>(p => p.Add(s => s.Tag, Uri.EscapeDataString("C#")));
4444
cut.WaitForState(() => cut.FindAll(".blog-card").Any());
4545

@@ -54,4 +54,4 @@ private async Task AddBlogPostWithTagAsync(string tag)
5454
await DbContext.AddAsync(blogPost);
5555
await DbContext.SaveChangesAsync();
5656
}
57-
}
57+
}

tests/LinkDotNet.Blog.IntegrationTests/Web/Pages/SearchTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async Task ShouldFindBlogPostWhenTitleMatches()
2525
await Repository.StoreAsync(blogPost2);
2626
using var ctx = new TestContext();
2727
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
28-
ctx.Services.AddScoped(_ => new Mock<IUserRecordService>().Object);
28+
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
2929

3030
var cut = ctx.RenderComponent<Search>(p => p.Add(s => s.SearchTerm, "Title 1"));
3131

@@ -44,7 +44,7 @@ public async Task ShouldFindBlogPostWhenTagMatches()
4444
await Repository.StoreAsync(blogPost2);
4545
using var ctx = new TestContext();
4646
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
47-
ctx.Services.AddScoped(_ => new Mock<IUserRecordService>().Object);
47+
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
4848

4949
var cut = ctx.RenderComponent<Search>(p => p.Add(s => s.SearchTerm, "Cat"));
5050

@@ -61,7 +61,7 @@ public async Task ShouldUnescapeQuery()
6161
await Repository.StoreAsync(blogPost1);
6262
using var ctx = new TestContext();
6363
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
64-
ctx.Services.AddScoped(_ => new Mock<IUserRecordService>().Object);
64+
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
6565

6666
var cut = ctx.RenderComponent<Search>(p => p.Add(s => s.SearchTerm, "Title%201"));
6767

@@ -70,4 +70,4 @@ public async Task ShouldUnescapeQuery()
7070
blogPosts.Should().HaveCount(1);
7171
blogPosts.Single().Find(".description h1").TextContent.Should().Be("Title 1");
7272
}
73-
}
73+
}

tests/LinkDotNet.Blog.IntegrationTests/Web/Shared/Admin/BlogPostAdminActionsTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void ShouldDeleteBlogPostWhenOkClicked()
2222
using var ctx = new TestContext();
2323
ctx.AddTestAuthorization().SetAuthorized("s");
2424
ctx.Services.AddSingleton(repositoryMock.Object);
25-
ctx.Services.AddSingleton(new Mock<IToastService>().Object);
25+
ctx.Services.AddSingleton(Mock.Of<IToastService>());
2626
var cut = ctx.RenderComponent<BlogPostAdminActions>(s => s.Add(p => p.BlogPostId, blogPostId));
2727
cut.Find("#delete-blogpost").Click();
2828

@@ -39,7 +39,7 @@ public void ShouldNotDeleteBlogPostWhenCancelClicked()
3939
using var ctx = new TestContext();
4040
ctx.AddTestAuthorization().SetAuthorized("s");
4141
ctx.Services.AddSingleton(repositoryMock.Object);
42-
ctx.Services.AddSingleton(new Mock<IToastService>().Object);
42+
ctx.Services.AddSingleton(Mock.Of<IToastService>());
4343
var cut = ctx.RenderComponent<BlogPostAdminActions>(s => s.Add(p => p.BlogPostId, blogPostId));
4444
cut.Find("#delete-blogpost").Click();
4545

@@ -56,12 +56,12 @@ public void ShouldGoToEditPageOnEditClick()
5656
using var ctx = new TestContext();
5757
ctx.AddTestAuthorization().SetAuthorized("s");
5858
ctx.Services.AddSingleton(repositoryMock.Object);
59-
ctx.Services.AddSingleton(new Mock<IToastService>().Object);
59+
ctx.Services.AddSingleton(Mock.Of<IToastService>());
6060
var navigationManager = ctx.Services.GetRequiredService<NavigationManager>();
6161
var cut = ctx.RenderComponent<BlogPostAdminActions>(s => s.Add(p => p.BlogPostId, blogPostId));
6262

6363
cut.Find("#edit-blogpost").Click();
6464

6565
navigationManager.Uri.Should().EndWith($"update/{blogPostId}");
6666
}
67-
}
67+
}

tests/LinkDotNet.Blog.IntegrationTests/Web/Shared/Skills/SkillTableTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public async Task ShouldDeleteItem()
2323
using var ctx = new TestContext();
2424
await Repository.StoreAsync(skill);
2525
ctx.Services.AddScoped<IRepository<Skill>>(_ => Repository);
26-
ctx.Services.AddScoped(_ => new Mock<IToastService>().Object);
26+
ctx.Services.AddScoped(_ => Mock.Of<IToastService>());
2727
var cut = ctx.RenderComponent<SkillTable>(p =>
2828
p.Add(s => s.IsAuthenticated, true));
2929
cut.WaitForState(() => cut.HasComponent<SkillTag>());
@@ -40,7 +40,7 @@ public async Task ShouldAddSkill()
4040
{
4141
using var ctx = new TestContext();
4242
ctx.Services.AddScoped<IRepository<Skill>>(_ => Repository);
43-
ctx.Services.AddScoped(_ => new Mock<IToastService>().Object);
43+
ctx.Services.AddScoped(_ => Mock.Of<IToastService>());
4444
var cut = ctx.RenderComponent<SkillTable>(p =>
4545
p.Add(s => s.IsAuthenticated, true));
4646
cut.Find("button").Click();
@@ -61,4 +61,4 @@ public async Task ShouldAddSkill()
6161
fromRepo.Capability.Should().Be("capability");
6262
fromRepo.ProficiencyLevel.Should().Be(ProficiencyLevel.Expert);
6363
}
64-
}
64+
}

tests/LinkDotNet.Blog.UnitTests/Web/Pages/AboutMeTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ private void SetupMocks(AppConfiguration config)
9696
.ReturnsAsync(new PagedList<ProfileInformationEntry>(Array.Empty<ProfileInformationEntry>(), 1, 1));
9797

9898
Services.AddScoped(_ => config);
99-
Services.AddScoped(_ => new Mock<IUserRecordService>().Object);
100-
Services.AddScoped(_ => new Mock<ISortOrderCalculator>().Object);
99+
Services.AddScoped(_ => Mock.Of<IUserRecordService>());
100+
Services.AddScoped(_ => Mock.Of<ISortOrderCalculator>());
101101
Services.AddScoped(_ => skillRepo.Object);
102102
Services.AddScoped(_ => profileRepo.Object);
103-
Services.AddScoped(_ => new Mock<IToastService>().Object);
103+
Services.AddScoped(_ => Mock.Of<IToastService>());
104104
}
105-
}
105+
}

tests/LinkDotNet.Blog.UnitTests/Web/Pages/Admin/DashboardTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void ShouldNotShowAboutMeStatisticsWhenDisabled()
2929
this.AddTestAuthorization().SetAuthorized("test");
3030
Services.AddScoped(_ => CreateAppConfiguration(false));
3131
Services.AddScoped(_ => dashboardService.Object);
32-
Services.AddScoped(_ => new Mock<IRepository<BlogPost>>().Object);
32+
Services.AddScoped(_ => Mock.Of<IRepository<BlogPost>>());
3333
Services.AddScoped(_ => new BlogDbContext(options));
3434
dashboardService.Setup(d => d.GetDashboardDataAsync())
3535
.ReturnsAsync(new DashboardData());
@@ -58,4 +58,4 @@ private static DbConnection CreateInMemoryConnection()
5858

5959
return connection;
6060
}
61-
}
61+
}

0 commit comments

Comments
 (0)