Skip to content

Commit 67f6ba2

Browse files
committed
Fixed test setup
1 parent f73b200 commit 67f6ba2

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.EntityFrameworkCore;
1212
using Microsoft.Extensions.DependencyInjection;
1313
using Moq;
14+
using Toolbelt.Blazor.HeadElement;
1415
using Xunit;
1516

1617
namespace LinkDotNet.Blog.IntegrationTests.Web.Pages
@@ -24,9 +25,7 @@ public async Task ShouldAddLikeOnEvent()
2425
await BlogPostRepository.StoreAsync(publishedPost);
2526
using var ctx = new TestContext();
2627
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
27-
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
28-
ctx.Services.AddScoped(_ => new Mock<ILocalStorageService>().Object);
29-
ctx.Services.AddScoped(_ => new Mock<IToastService>().Object);
28+
RegisterComponents(ctx);
3029
ctx.AddTestAuthorization().SetAuthorized("s");
3130
var cut = ctx.RenderComponent<BlogPostPage>(
3231
p => p.Add(b => b.BlogPostId, publishedPost.Id));
@@ -49,9 +48,7 @@ public async Task ShouldSubtractLikeOnEvent()
4948
localStorage.Setup(l => l.ContainKeyAsync("hasLiked", default)).ReturnsAsync(true);
5049
localStorage.Setup(l => l.GetItemAsync<bool>("hasLiked", default)).ReturnsAsync(true);
5150
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
52-
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
53-
ctx.Services.AddScoped(_ => localStorage.Object);
54-
ctx.Services.AddScoped(_ => new Mock<IToastService>().Object);
51+
RegisterComponents(ctx, localStorage.Object);
5552
ctx.AddTestAuthorization().SetAuthorized("s");
5653
var cut = ctx.RenderComponent<BlogPostPage>(
5754
p => p.Add(b => b.BlogPostId, publishedPost.Id));
@@ -72,15 +69,21 @@ public async Task ShouldSetTagsWhenAvailable()
7269
using var ctx = new TestContext();
7370
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
7471
ctx.AddTestAuthorization();
75-
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
76-
ctx.Services.AddScoped(_ => new Mock<ILocalStorageService>().Object);
77-
ctx.Services.AddScoped(_ => new Mock<IToastService>().Object);
72+
RegisterComponents(ctx);
7873
var cut = ctx.RenderComponent<BlogPostPage>(
7974
p => p.Add(b => b.BlogPostId, publishedPost.Id));
8075

8176
var ogData = cut.FindComponent<OgData>();
8277

8378
ogData.Instance.Keywords.Should().Be("Tag1,Tag2");
8479
}
80+
81+
private void RegisterComponents(TestContextBase ctx, ILocalStorageService localStorageService = null)
82+
{
83+
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
84+
ctx.Services.AddScoped(_ => localStorageService ?? new Mock<ILocalStorageService>().Object);
85+
ctx.Services.AddScoped(_ => new Mock<IToastService>().Object);
86+
ctx.Services.AddScoped(_ => new Mock<IHeadElementHelper>().Object);
87+
}
8588
}
8689
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using LinkDotNet.Domain;
1010
using LinkDotNet.Infrastructure.Persistence;
1111
using Microsoft.Extensions.DependencyInjection;
12+
using Moq;
13+
using Toolbelt.Blazor.HeadElement;
1214
using Xunit;
1315

1416
namespace LinkDotNet.Blog.IntegrationTests.Web.Pages
@@ -24,8 +26,7 @@ public async Task ShouldShowAllBlogPostsWithLatestOneFirst()
2426
await BlogPostRepository.StoreAsync(newestBlogPost);
2527
using var ctx = new TestContext();
2628
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
27-
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
28-
ctx.Services.AddScoped(_ => CreateSampleAppConfiguration());
29+
RegisterComponents(ctx);
2930
var cut = ctx.RenderComponent<Index>();
3031
cut.WaitForState(() => cut.FindAll(".blog-card").Any());
3132

@@ -45,8 +46,7 @@ public async Task ShouldOnlyShowPublishedPosts()
4546
await BlogPostRepository.StoreAsync(unpublishedPost);
4647
using var ctx = new TestContext();
4748
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
48-
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
49-
ctx.Services.AddScoped(_ => CreateSampleAppConfiguration());
49+
RegisterComponents(ctx);
5050
var cut = ctx.RenderComponent<Index>();
5151
cut.WaitForState(() => cut.FindAll(".blog-card").Any());
5252

@@ -62,8 +62,7 @@ public async Task ShouldOnlyLoadTenEntities()
6262
await CreatePublishedBlogPosts(11);
6363
using var ctx = new TestContext();
6464
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
65-
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
66-
ctx.Services.AddScoped(_ => CreateSampleAppConfiguration());
65+
RegisterComponents(ctx);
6766
var cut = ctx.RenderComponent<Index>();
6867
cut.WaitForState(() => cut.FindAll(".blog-card").Any());
6968

@@ -78,8 +77,7 @@ public async Task ShouldLoadNextBatchOnClick()
7877
await CreatePublishedBlogPosts(11);
7978
using var ctx = new TestContext();
8079
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
81-
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
82-
ctx.Services.AddScoped(_ => CreateSampleAppConfiguration());
80+
RegisterComponents(ctx);
8381
var cut = ctx.RenderComponent<Index>();
8482

8583
cut.FindComponent<BlogPostNavigation>().Find("li:last-child a").Click();
@@ -95,8 +93,7 @@ public async Task ShouldLoadPreviousBatchOnClick()
9593
await CreatePublishedBlogPosts(11);
9694
using var ctx = new TestContext();
9795
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
98-
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
99-
ctx.Services.AddScoped(_ => CreateSampleAppConfiguration());
96+
RegisterComponents(ctx);
10097
var cut = ctx.RenderComponent<Index>();
10198
cut.WaitForState(() => cut.FindAll(".blog-card").Any());
10299
cut.FindComponent<BlogPostNavigation>().Find("li:last-child a").Click();
@@ -132,5 +129,12 @@ private async Task CreatePublishedBlogPosts(int amount)
132129
await BlogPostRepository.StoreAsync(blogPost);
133130
}
134131
}
132+
133+
private void RegisterComponents(TestContextBase ctx)
134+
{
135+
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
136+
ctx.Services.AddScoped(_ => CreateSampleAppConfiguration());
137+
ctx.Services.AddScoped(_ => new Mock<IHeadElementHelper>().Object);
138+
}
135139
}
136140
}

0 commit comments

Comments
 (0)