Skip to content

Commit 18dbb62

Browse files
committed
Added Tests for SitemapService
1 parent 684841d commit 18dbb62

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
using System.Threading.Tasks;
4+
using Bunit;
5+
using Bunit.TestDoubles;
6+
using FluentAssertions;
7+
using LinkDotNet.Blog.Domain;
8+
using LinkDotNet.Blog.Infrastructure.Persistence;
9+
using LinkDotNet.Blog.TestUtilities;
10+
using LinkDotNet.Blog.Web.Shared.Services;
11+
using LinkDotNet.Blog.Web.Shared.Services.Sitemap;
12+
using Moq;
13+
using X.PagedList;
14+
using Xunit;
15+
16+
namespace LinkDotNet.Blog.UnitTests.Web.Shared.Services
17+
{
18+
public class SitemapServiceTests : TestContext
19+
{
20+
private readonly Mock<IRepository<BlogPost>> repositoryMock;
21+
private readonly Mock<IXmlFileWriter> xmlFileWriterMock;
22+
private readonly SitemapService sut;
23+
private readonly FakeNavigationManager fakeNavigationManager;
24+
25+
public SitemapServiceTests()
26+
{
27+
repositoryMock = new Mock<IRepository<BlogPost>>();
28+
fakeNavigationManager = new FakeNavigationManager(Renderer);
29+
30+
xmlFileWriterMock = new Mock<IXmlFileWriter>();
31+
sut = new SitemapService(
32+
repositoryMock.Object,
33+
fakeNavigationManager,
34+
xmlFileWriterMock.Object);
35+
}
36+
37+
[Fact]
38+
public async Task ShouldCreateSitemap()
39+
{
40+
var bp1 = new BlogPostBuilder()
41+
.WithUpdatedDate(new DateTime(2020, 1, 1))
42+
.WithTags("tag1", "tag2")
43+
.Build();
44+
bp1.Id = "id1";
45+
var bp2 = new BlogPostBuilder()
46+
.WithUpdatedDate(new DateTime(2019, 1, 1))
47+
.WithTags("tag2")
48+
.Build();
49+
bp2.Id = "id2";
50+
var blogPosts = new[] { bp1, bp2 };
51+
repositoryMock.Setup(r => r.GetAllAsync(
52+
It.IsAny<Expression<Func<BlogPost, bool>>>(),
53+
p => p.UpdatedDate,
54+
true,
55+
It.IsAny<int>(),
56+
It.IsAny<int>()))
57+
.ReturnsAsync(new PagedList<BlogPost>(blogPosts, 1, 10));
58+
59+
var sitemap = await sut.CreateSitemapAsync();
60+
61+
sitemap.Namespace.Should().Be("http://www.sitemaps.org/schemas/sitemap/0.9");
62+
sitemap.Urls.Should().HaveCount(5);
63+
sitemap.Urls[0].Location.Should().Be($"{fakeNavigationManager.BaseUri}");
64+
sitemap.Urls[1].Location.Should().Be($"{fakeNavigationManager.BaseUri}blogPost/id1");
65+
sitemap.Urls[2].Location.Should().Be($"{fakeNavigationManager.BaseUri}blogPost/id2");
66+
sitemap.Urls[3].Location.Should().Be($"{fakeNavigationManager.BaseUri}searchByTag/tag1");
67+
sitemap.Urls[4].Location.Should().Be($"{fakeNavigationManager.BaseUri}searchByTag/tag2");
68+
}
69+
70+
[Fact]
71+
public async Task ShouldSaveSitemapToFile()
72+
{
73+
var sitemap = new SitemapUrlSet();
74+
75+
await sut.SaveSitemapToFileAsync(sitemap);
76+
77+
xmlFileWriterMock.Verify(x => x.WriteObjectToXmlFileAsync(sitemap, "sitemap.xml"));
78+
}
79+
}
80+
}

LinkDotNet.Blog.Web/Shared/Services/Sitemap/SitemapService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private IEnumerable<SitemapUrl> CreateUrlsForBlogPosts(IEnumerable<BlogPost> blo
5050
{
5151
return blogPosts.Select(b => new SitemapUrl
5252
{
53-
Location = $"{navigationManager.BaseUri}blogPosts/{b.Id}",
53+
Location = $"{navigationManager.BaseUri}blogPost/{b.Id}",
5454
LastModified = b.UpdatedDate.ToString("yyyy-MM-dd"),
5555
}).ToList();
5656
}

0 commit comments

Comments
 (0)