Skip to content

Commit ed667f5

Browse files
authored
Merge pull request #101 from lakatoshv/v3.7.1_AddExceptionTestsCasesForServicesTests
V3.7.1 add exception tests cases for services tests
2 parents 83bcfa4 + 679bbcf commit ed667f5

File tree

6 files changed

+472
-16
lines changed

6 files changed

+472
-16
lines changed

BlogWebApp/Tests/Blog.ServicesTests/EntityServices/CommentsServiceTests.cs

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
51
using AutoFixture;
62
using AutoFixture.Dsl;
73
using Blog.Core.Enums;
@@ -15,6 +11,10 @@
1511
using Blog.EntityServices.Interfaces;
1612
using Microsoft.EntityFrameworkCore;
1713
using Moq;
14+
using System;
15+
using System.Collections.Generic;
16+
using System.Linq;
17+
using System.Threading.Tasks;
1818
using Xunit;
1919

2020
namespace Blog.ServicesTests.EntityServices;
@@ -1261,6 +1261,23 @@ public void Update_WhenCommentExists_ShouldReturnComment(string newCommentBody)
12611261
Assert.Equal(newCommentBody, comment.CommentBody);
12621262
}
12631263

1264+
/// <summary>
1265+
/// Update comment.
1266+
/// Should throw exception when repository throws exception.
1267+
/// </summary>
1268+
[Fact]
1269+
public void Update_WhenRepositoryThrowsException_ShouldThrowException()
1270+
{
1271+
//Arrange
1272+
var comment = SetupCommentFixture().Create();
1273+
1274+
_commentsRepositoryMock.Setup(x => x.Update(It.IsAny<Comment>()))
1275+
.Throws(new Exception("Test exception"));
1276+
1277+
//Assert
1278+
Assert.Throws<Exception>(() => _commentsService.Update(comment));
1279+
}
1280+
12641281
#endregion
12651282

12661283
#region Upadate Enumerable function
@@ -1344,6 +1361,27 @@ public void UpdateEnumerable_WhenCommentExists_ShouldReturnComment(string newCom
13441361
}
13451362
}
13461363

1364+
/// <summary>
1365+
/// Update Enumerable comments.
1366+
/// Should throw exception when repository throws exception.
1367+
/// </summary>
1368+
[Fact]
1369+
public void UpdateEnumerable_WhenRepositoryThrowsException_ShouldThrowException()
1370+
{
1371+
//Arrange
1372+
var random = new Random();
1373+
var itemsCount = random.Next(100);
1374+
var comments = SetupCommentFixture()
1375+
.CreateMany(itemsCount)
1376+
.ToList();
1377+
1378+
_commentsRepositoryMock.Setup(x => x.Update(It.IsAny<IEnumerable<Comment>>()))
1379+
.Throws(new Exception("Test exception"));
1380+
1381+
//Assert
1382+
Assert.Throws<Exception>(() => _commentsService.Update(comments));
1383+
}
1384+
13471385
#endregion
13481386

13491387
#region Update Async function
@@ -1413,6 +1451,23 @@ public async Task UpdateAsync_WhenCommentExists_ShouldReturnComment(string newCo
14131451
Assert.Equal(newCommentBody, comment.CommentBody);
14141452
}
14151453

1454+
/// <summary>
1455+
/// Async Update comment.
1456+
/// Should throw exception when repository throws exception.
1457+
/// </summary>
1458+
[Fact]
1459+
public async Task UpdateAsync_WhenRepositoryThrowsException_ShouldThrowException()
1460+
{
1461+
//Arrange
1462+
var comment = SetupCommentFixture().Create();
1463+
1464+
_commentsRepositoryMock.Setup(x => x.UpdateAsync(It.IsAny<Comment>()))
1465+
.ThrowsAsync(new Exception("Test exception"));
1466+
1467+
//Assert
1468+
await Assert.ThrowsAsync<Exception>(() => _commentsService.UpdateAsync(comment));
1469+
}
1470+
14161471
#endregion
14171472

14181473
#region Upadate Async Enumerable function
@@ -1498,6 +1553,27 @@ public async Task UpdateAsyncEnumerable_WhenCommentExists_ShouldReturnComment(st
14981553
}
14991554
}
15001555

1556+
/// <summary>
1557+
/// Update Async Enumerable comments.
1558+
/// Should throw exception when repository throws exception.
1559+
/// </summary>
1560+
[Fact]
1561+
public async Task UpdateAsyncEnumerable_WhenRepositoryThrowsException_ShouldThrowException()
1562+
{
1563+
//Arrange
1564+
var random = new Random();
1565+
var itemsCount = random.Next(100);
1566+
var comments = SetupCommentFixture()
1567+
.CreateMany(itemsCount)
1568+
.ToList();
1569+
1570+
_commentsRepositoryMock.Setup(x => x.UpdateAsync(It.IsAny<IEnumerable<Comment>>()))
1571+
.ThrowsAsync(new Exception("Test exception"));
1572+
1573+
//Assert
1574+
await Assert.ThrowsAsync<Exception>(() => _commentsService.UpdateAsync(comments));
1575+
}
1576+
15011577
#endregion
15021578

15031579
#endregion

BlogWebApp/Tests/Blog.ServicesTests/EntityServices/MessagesServiceTests.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,23 @@ public void Update_WhenMessageExists_ShouldReturnMessage(string newMessageSubjec
12841284
Assert.Equal(newMessageSubject, message.Subject);
12851285
}
12861286

1287+
/// <summary>
1288+
/// Update message.
1289+
/// Should throw exception when repository throws exception.
1290+
/// </summary>
1291+
[Fact]
1292+
public void Update_WhenRepositoryThrowsException_ShouldThrowException()
1293+
{
1294+
//Arrange
1295+
var message = SetupMessageFixture().Create();
1296+
1297+
_messagesRepositoryMock.Setup(x => x.Update(It.IsAny<Message>()))
1298+
.Throws(new Exception("Test exception"));
1299+
1300+
//Assert
1301+
Assert.Throws<Exception>(() => _messagesService.Update(message));
1302+
}
1303+
12871304
#endregion
12881305

12891306
#region Upadate Enumerable function
@@ -1368,6 +1385,27 @@ public void UpdateEnumerable_WhenMessageExists_ShouldReturnMessage(string newMes
13681385
}
13691386
}
13701387

1388+
/// <summary>
1389+
/// Update Enumerable messages.
1390+
/// Should throw exception when repository throws exception.
1391+
/// </summary>
1392+
[Fact]
1393+
public void UpdateEnumerable_WhenRepositoryThrowsException_ShouldThrowException()
1394+
{
1395+
//Arrange
1396+
var random = new Random();
1397+
var itemsCount = random.Next(100);
1398+
var messages = SetupMessageFixture()
1399+
.CreateMany(itemsCount)
1400+
.ToList();
1401+
1402+
_messagesRepositoryMock.Setup(x => x.Update(It.IsAny<IEnumerable<Message>>()))
1403+
.Throws(new Exception("Test exception"));
1404+
1405+
//Assert
1406+
Assert.Throws<Exception>(() => _messagesService.Update(messages));
1407+
}
1408+
13711409
#endregion
13721410

13731411
#region Update Async function
@@ -1440,6 +1478,23 @@ public async Task UpdateAsync_WhenMessageExists_ShouldReturnMessage(string newMe
14401478
Assert.Equal(newMessageSubject, message.Subject);
14411479
}
14421480

1481+
/// <summary>
1482+
/// Async Update message.
1483+
/// Should throw exception when repository throws exception.
1484+
/// </summary>
1485+
[Fact]
1486+
public async Task UpdateAsync_WhenRepositoryThrowsException_ShouldThrowException()
1487+
{
1488+
//Arrange
1489+
var message = SetupMessageFixture().Create();
1490+
1491+
_messagesRepositoryMock.Setup(x => x.UpdateAsync(It.IsAny<Message>()))
1492+
.ThrowsAsync(new Exception("Test exception"));
1493+
1494+
//Assert
1495+
await Assert.ThrowsAsync<Exception>(() => _messagesService.UpdateAsync(message));
1496+
}
1497+
14431498
#endregion
14441499

14451500
#region Upadate Async Enumerable function
@@ -1526,6 +1581,27 @@ public async Task UpdateAsyncEnumerable_WhenMessageExists_ShouldReturnMessage(st
15261581
}
15271582
}
15281583

1584+
/// <summary>
1585+
/// Update Async Enumerable messages.
1586+
/// Should throw exception when repository throws exception.
1587+
/// </summary>
1588+
[Fact]
1589+
public async Task UpdateAsyncEnumerable_WhenRepositoryThrowsException_ShouldThrowException()
1590+
{
1591+
//Arrange
1592+
var random = new Random();
1593+
var itemsCount = random.Next(100);
1594+
var messages = SetupMessageFixture()
1595+
.CreateMany(itemsCount)
1596+
.ToList();
1597+
1598+
_messagesRepositoryMock.Setup(x => x.UpdateAsync(It.IsAny<IEnumerable<Message>>()))
1599+
.ThrowsAsync(new Exception("Test exception"));
1600+
1601+
//Assert
1602+
await Assert.ThrowsAsync<Exception>(() => _messagesService.UpdateAsync(messages));
1603+
}
1604+
15291605
#endregion
15301606

15311607
#endregion

BlogWebApp/Tests/Blog.ServicesTests/EntityServices/PostTagRelationsServiceTests.cs

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
51
using AutoFixture;
62
using AutoFixture.Dsl;
73
using Blog.Core.Enums;
@@ -15,6 +11,10 @@
1511
using Blog.EntityServices.Interfaces;
1612
using Microsoft.EntityFrameworkCore;
1713
using Moq;
14+
using System;
15+
using System.Collections.Generic;
16+
using System.Linq;
17+
using System.Threading.Tasks;
1818
using Xunit;
1919

2020
namespace Blog.ServicesTests.EntityServices;
@@ -1673,6 +1673,23 @@ public void Update_WhenPostTagRelationsExists_ShouldReturnPostTagRelation()
16731673
Assert.Equal(postsTagsRelations.Tag.Title, newPostsTagsRelation.Tag.Title);
16741674
}
16751675

1676+
/// <summary>
1677+
/// Update post tag relations.
1678+
/// Should throw exception when repository throws exception.
1679+
/// </summary>
1680+
[Fact]
1681+
public void Update_WhenRepositoryThrowsException_ShouldThrowException()
1682+
{
1683+
//Arrange
1684+
var postsTagsRelation = SetupPostsTagsRelationsFixture().Create();
1685+
1686+
_postsTagsRelationsRepositoryMock.Setup(x => x.Update(It.IsAny<PostsTagsRelations>()))
1687+
.Throws(new Exception("Test exception"));
1688+
1689+
//Assert
1690+
Assert.Throws<Exception>(() => _postsTagsRelationsService.Update(postsTagsRelation));
1691+
}
1692+
16761693
#endregion
16771694

16781695
#region Upadate Enumerable function
@@ -1756,6 +1773,27 @@ public void UpdateEnumerable_WhenPostTagRelationsExists_ShouldReturnPostTagRelat
17561773
});
17571774
}
17581775

1776+
/// <summary>
1777+
/// Update Enumerable post tag relations.
1778+
/// Should throw exception when repository throws exception.
1779+
/// </summary>
1780+
[Fact]
1781+
public void UpdateEnumerable_WhenRepositoryThrowsException_ShouldThrowException()
1782+
{
1783+
//Arrange
1784+
var random = new Random();
1785+
var itemsCount = random.Next(100);
1786+
var postsTagsRelations = SetupPostsTagsRelationsFixture()
1787+
.CreateMany(itemsCount)
1788+
.ToList();
1789+
1790+
_postsTagsRelationsRepositoryMock.Setup(x => x.Update(It.IsAny<IEnumerable<PostsTagsRelations>>()))
1791+
.Throws(new Exception("Test exception"));
1792+
1793+
//Assert
1794+
Assert.Throws<Exception>(() => _postsTagsRelationsService.Update(postsTagsRelations));
1795+
}
1796+
17591797
#endregion
17601798

17611799
#region Update Async function
@@ -1895,6 +1933,23 @@ public async Task UpdateAsync_WhenPostTagRelationExists_ShouldReturnPostTagRelat
18951933
Assert.Equal(postsTagsRelations.Tag.Title, newTag.Title);
18961934
}
18971935

1936+
/// <summary>
1937+
/// Async Update post tag relation.
1938+
/// Should throw exception when repository throws exception.
1939+
/// </summary>
1940+
[Fact]
1941+
public async Task UpdateAsync_WhenRepositoryThrowsException_ShouldThrowException()
1942+
{
1943+
//Arrange
1944+
var postsTagsRelation = SetupPostsTagsRelationsFixture().Create();
1945+
1946+
_postsTagsRelationsRepositoryMock.Setup(x => x.UpdateAsync(It.IsAny<PostsTagsRelations>()))
1947+
.ThrowsAsync(new Exception("Test exception"));
1948+
1949+
//Assert
1950+
await Assert.ThrowsAsync<Exception>(() => _postsTagsRelationsService.UpdateAsync(postsTagsRelation));
1951+
}
1952+
18981953
#endregion
18991954

19001955
#region Upadate Async Enumerable function
@@ -1980,6 +2035,27 @@ public async Task UpdateAsyncEnumerable_WhenPostTagRelationsExists_ShouldReturnP
19802035
});
19812036
}
19822037

2038+
/// <summary>
2039+
/// Update Async Enumerable post tag relations.
2040+
/// Should throw exception when repository throws exception.
2041+
/// </summary>
2042+
[Fact]
2043+
public async Task UpdateAsyncEnumerable_WhenRepositoryThrowsException_ShouldThrowException()
2044+
{
2045+
//Arrange
2046+
var random = new Random();
2047+
var itemsCount = random.Next(100);
2048+
var postsTagsRelations = SetupPostsTagsRelationsFixture()
2049+
.CreateMany(itemsCount)
2050+
.ToList();
2051+
2052+
_postsTagsRelationsRepositoryMock.Setup(x => x.UpdateAsync(It.IsAny<IEnumerable<PostsTagsRelations>>()))
2053+
.ThrowsAsync(new Exception("Test exception"));
2054+
2055+
//Assert
2056+
await Assert.ThrowsAsync<Exception>(() => _postsTagsRelationsService.UpdateAsync(postsTagsRelations));
2057+
}
2058+
19832059
#endregion
19842060

19852061
#endregion

0 commit comments

Comments
 (0)