Skip to content

Commit f1640ea

Browse files
committed
Removed FluentAssertions and added Shouldly as it provides more readable assertions (#326)
1 parent ba1ab4b commit f1640ea

File tree

7 files changed

+58
-90
lines changed

7 files changed

+58
-90
lines changed

samples/Blog/Blog.Test/Blog.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="FluentAssertions" Version="5.9.0" />
1615
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0-*" />
1716
<PackageReference Include="Moq" Version="4.13.0" />
17+
<PackageReference Include="Shouldly" Version="4.0.0-*" />
1818
<PackageReference Include="xunit" Version="2.4.1" />
1919
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
2020
<PrivateAssets>all</PrivateAssets>

samples/Blog/Blog.Test/Controllers/Admin/ArticlesControllerTest.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
using Blog.Controllers;
77
using Blog.Data.Models;
88
using Data;
9-
using FluentAssertions;
109
using MyTested.AspNetCore.Mvc;
1110
using Services.Models;
11+
using Shouldly;
1212
using Xunit;
1313

1414
using ArticlesController = Web.Areas.Admin.Controllers.ArticlesController;
@@ -32,9 +32,7 @@ public void AllShouldReturnViewWithAllArticles()
3232
.ShouldReturn()
3333
.View(view => view
3434
.WithModelOfType<List<ArticleNonPublicListingServiceModel>>()
35-
.Passing(articles => articles
36-
.Should()
37-
.NotBeEmpty()));
35+
.Passing(articles => articles.ShouldNotBeEmpty()));
3836

3937
[Fact]
4038
public void ChangeVisibilityShouldChangeArticleAndRedirectToAll()
@@ -44,19 +42,14 @@ public void ChangeVisibilityShouldChangeArticleAndRedirectToAll()
4442
.Calling(c => c.ChangeVisibility(1))
4543
.ShouldHave()
4644
.Data(data => data
47-
.WithSet<Article>(set => set
48-
.FirstOrDefault(a => a.IsPublic)
49-
.Should()
50-
.NotBeNull()
51-
.And
52-
.Subject
53-
.As<Article>()
54-
.PublishedOn
55-
.Should()
56-
.NotBeNull()
57-
.And
58-
.Should()
59-
.Be(new DateTime(1, 1, 1))))
45+
.WithSet<Article>(set =>
46+
{
47+
var article = set.FirstOrDefault(a => a.IsPublic);
48+
49+
article.ShouldNotBeNull();
50+
article.PublishedOn.ShouldNotBeNull();
51+
article.PublishedOn.ShouldBe(new DateTime(1, 1, 1));
52+
}))
6053
.AndAlso()
6154
.ShouldReturn()
6255
.Redirect(redirect => redirect

samples/Blog/Blog.Test/Controllers/ArticlesControllerTest.cs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
using Blog.Controllers.Models;
66
using Blog.Data.Models;
77
using Data;
8-
using FluentAssertions;
98
using MyTested.AspNetCore.Mvc;
109
using Services;
1110
using Services.Models;
1211
using System.Linq;
1312
using Xunit;
13+
using Shouldly;
1414

1515
public class ArticlesControllerTest
1616
{
@@ -28,9 +28,9 @@ public void AllShouldReturnDefaultViewWithCorrectModel(int total, int page, int
2828
.WithModelOfType<ArticleListingViewModel>()
2929
.Passing(articleListing =>
3030
{
31-
articleListing.Articles.Should().HaveCount(expectedCount);
32-
articleListing.Total.Should().Be(total);
33-
articleListing.Page.Should().Be(page);
31+
articleListing.Articles.Count().ShouldBe(expectedCount);
32+
articleListing.Total.ShouldBe(total);
33+
articleListing.Page.ShouldBe(page);
3434
}));
3535

3636
[Fact]
@@ -151,15 +151,11 @@ public void CreatePostShouldSaveArticleSetTempDataMessageAndRedirectWhenValidMod
151151
.AndAlso()
152152
.ShouldHave()
153153
.Data(data => data
154-
.WithSet<Article>(set => set
155-
.Should()
156-
.NotBeEmpty()
157-
.And
158-
.Subject
159-
.SingleOrDefault(a => a.Title == title)
160-
.Should()
161-
.NotBeNull()
162-
))
154+
.WithSet<Article>(set =>
155+
{
156+
set.ShouldNotBeEmpty();
157+
set.SingleOrDefault(a => a.Title == title).ShouldNotBeNull();
158+
}))
163159
.AndAlso()
164160
.ShouldHave()
165161
.TempData(tempData => tempData
@@ -281,13 +277,13 @@ public void EditPostShouldSaveArticleSetTempDataMessageAndRedirectWhenValidModel
281277
.Data(data => data
282278
.WithSet<Article>(set =>
283279
{
284-
set.Should().NotBeEmpty();
280+
set.ShouldNotBeEmpty();
285281

286282
var article = set.SingleOrDefault(a => a.Id == articleId);
287283

288-
article.Should().NotBeNull();
289-
article.Title.Should().Be($"Edit {title}");
290-
article.Content.Should().Be($"Edit {content}");
284+
article.ShouldNotBeNull();
285+
article.Title.ShouldBe($"Edit {title}");
286+
article.Content.ShouldBe($"Edit {content}");
291287
}))
292288
.AndAlso()
293289
.ShouldHave()
@@ -378,7 +374,7 @@ public void ConfirmDeleteShouldDeleteArticleSetTempDataMessageAndRedirectWhenVal
378374
.Calling(c => c.ConfirmDelete(articleId))
379375
.ShouldHave()
380376
.Data(data => data
381-
.WithSet<Article>(set => set.Should().NotBeNull()))
377+
.WithSet<Article>(set => set.ShouldBeEmpty()))
382378
.AndAlso()
383379
.ShouldHave()
384380
.TempData(tempData => tempData
@@ -407,13 +403,10 @@ public void MineShouldReturnViewWithCorrectModel()
407403
.ShouldReturn()
408404
.View(view => view
409405
.WithModelOfType<List<ArticleForUserListingServiceModel>>()
410-
.Passing(articles => articles
411-
.Should()
412-
.NotBeEmpty()
413-
.And
414-
.Subject
415-
.SingleOrDefault(a => a.Author == "Author 1")
416-
.Should()
417-
.NotBeNull()));
406+
.Passing(articles =>
407+
{
408+
articles.ShouldNotBeEmpty();
409+
articles.SingleOrDefault(a => a.Author == "Author 1").ShouldNotBeNull();
410+
}));
418411
}
419412
}

samples/Blog/Blog.Test/Controllers/HomeControllerTest.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
using System.Collections.Generic;
44
using Blog.Controllers;
55
using Data;
6-
using FluentAssertions;
76
using MyTested.AspNetCore.Mvc;
87
using Services.Models;
8+
using Shouldly;
99
using Xunit;
1010

1111
public class HomeControllerTest
@@ -22,9 +22,7 @@ public void IndexShouldReturnDefaultViewWithCorrectModel(int total, bool arePubl
2222
.ShouldReturn()
2323
.View(view => view
2424
.WithModelOfType<List<ArticleListingServiceModel>>()
25-
.Passing(articles => articles
26-
.Should()
27-
.HaveCount(expected)));
25+
.Passing(articles => articles.Count.ShouldBe(expected)));
2826

2927
[Fact]
3028
public void PrivacyShouldReturnDefaultView()

samples/Blog/Blog.Test/Pipeline/Admin/ArticlesPipelineTest.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
using Blog.Controllers;
77
using Blog.Data.Models;
88
using Data;
9-
using FluentAssertions;
109
using MyTested.AspNetCore.Mvc;
1110
using Services.Models;
11+
using Shouldly;
1212
using Xunit;
1313

1414
using ArticlesController = Web.Areas.Admin.Controllers.ArticlesController;
@@ -28,9 +28,7 @@ public void GetAllShouldReturnViewWithAllArticles()
2828
.ShouldReturn()
2929
.View(view => view
3030
.WithModelOfType<List<ArticleNonPublicListingServiceModel>>()
31-
.Passing(articles => articles
32-
.Should()
33-
.NotBeEmpty()));
31+
.Passing(articles => articles.ShouldNotBeEmpty()));
3432

3533
[Fact]
3634
public void GetMineShouldChangeArticleAndRedirectToAll()
@@ -44,19 +42,14 @@ public void GetMineShouldChangeArticleAndRedirectToAll()
4442
.WithData(ArticleTestData.GetArticles(1, isPublic: false)))
4543
.ShouldHave()
4644
.Data(data => data
47-
.WithSet<Article>(set => set
48-
.FirstOrDefault(a => a.IsPublic)
49-
.Should()
50-
.NotBeNull()
51-
.And
52-
.Subject
53-
.As<Article>()
54-
.PublishedOn
55-
.Should()
56-
.NotBeNull()
57-
.And
58-
.Should()
59-
.Be(new DateTime(1, 1, 1))))
45+
.WithSet<Article>(set =>
46+
{
47+
var article = set.FirstOrDefault(a => a.IsPublic);
48+
49+
article.ShouldNotBeNull();
50+
article.PublishedOn.ShouldNotBeNull();
51+
article.PublishedOn.ShouldBe(new DateTime(1, 1, 1));
52+
}))
6053
.AndAlso()
6154
.ShouldReturn()
6255
.Redirect(redirect => redirect

samples/Blog/Blog.Test/Pipeline/ArticlesPipelineTest.cs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
using Blog.Controllers.Models;
77
using Blog.Data.Models;
88
using Data;
9-
using FluentAssertions;
109
using MyTested.AspNetCore.Mvc;
1110
using Services;
1211
using Services.Models;
12+
using Shouldly;
1313
using Xunit;
1414

1515
public class ArticlesPipelineTest
@@ -30,9 +30,9 @@ public void GetAllWithPageShouldReturnDefaultViewWithCorrectModel(int total, int
3030
.WithModelOfType<ArticleListingViewModel>()
3131
.Passing(articleListing =>
3232
{
33-
articleListing.Articles.Should().HaveCount(expectedCount);
34-
articleListing.Total.Should().Be(total);
35-
articleListing.Page.Should().Be(page);
33+
articleListing.Articles.Count().ShouldBe(expectedCount);
34+
articleListing.Total.ShouldBe(total);
35+
articleListing.Page.ShouldBe(page);
3636
}));
3737

3838
[Fact]
@@ -86,15 +86,11 @@ public void PostCreateShouldSaveArticleHaveValidModelStateAndRedirect(string tit
8686
.AndAlso()
8787
.ShouldHave()
8888
.Data(data => data
89-
.WithSet<Article>(set => set
90-
.Should()
91-
.NotBeEmpty()
92-
.And
93-
.Subject
94-
.SingleOrDefault(a => a.Title == title)
95-
.Should()
96-
.NotBeNull()
97-
))
89+
.WithSet<Article>(set =>
90+
{
91+
set.ShouldNotBeEmpty();
92+
set.SingleOrDefault(a => a.Title == title).ShouldNotBeNull();
93+
}))
9894
.AndAlso()
9995
.ShouldHave()
10096
.TempData(tempData => tempData
@@ -117,13 +113,10 @@ public void GetMineShouldReturnViewWithCorrectModel()
117113
.ShouldReturn()
118114
.View(view => view
119115
.WithModelOfType<List<ArticleForUserListingServiceModel>>()
120-
.Passing(articles => articles
121-
.Should()
122-
.NotBeEmpty()
123-
.And
124-
.Subject
125-
.SingleOrDefault(a => a.Author == "Author 1")
126-
.Should()
127-
.NotBeNull()));
116+
.Passing(articles =>
117+
{
118+
articles.ShouldNotBeEmpty();
119+
articles.SingleOrDefault(a => a.Author == "Author 1").ShouldNotBeNull();
120+
}));
128121
}
129122
}

samples/Blog/Blog.Test/Pipeline/HomePipelineTest.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
using System.Collections.Generic;
44
using Blog.Controllers;
55
using Data;
6-
using FluentAssertions;
76
using Services.Models;
87
using MyTested.AspNetCore.Mvc;
98
using Xunit;
9+
using Shouldly;
1010

1111
public class HomePipelineTest
1212
{
@@ -24,9 +24,7 @@ public void GetIndexShouldReturnDefaultViewWithCorrectModel(int total, bool areP
2424
.ShouldReturn()
2525
.View(view => view
2626
.WithModelOfType<List<ArticleListingServiceModel>>()
27-
.Passing(articles => articles
28-
.Should()
29-
.HaveCount(expected)));
27+
.Passing(articles => articles.Count.ShouldBe(expected)));
3028

3129
[Fact]
3230
public void GetPrivacyShouldReturnDefaultView()

0 commit comments

Comments
 (0)