Skip to content

Commit 37dc50f

Browse files
authored
Merge pull request #105 from lakatoshv/v3.7.5_AddExceptionCasesForAnyFunctionUnitTests
V3.7.5 add exception cases for any function unit tests
2 parents 857fbcf + fb7796e commit 37dc50f

File tree

6 files changed

+201
-0
lines changed

6 files changed

+201
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Collections.Generic;
1616
using System.Linq;
1717
using System.Threading.Tasks;
18+
using Blog.Data.Specifications.Base;
1819
using Xunit;
1920

2021
namespace Blog.ServicesTests.EntityServices;
@@ -2200,6 +2201,22 @@ public void Any_WithEqualSpecification_WhenCommentDoesNotExists_ShouldReturnNoth
22002201
Assert.False(areAnyComments);
22012202
}
22022203

2204+
/// <summary>
2205+
/// Any.
2206+
/// When repository throws exception should throw exception.
2207+
/// </summary>
2208+
[Fact]
2209+
public void Any_WhenRepositoryThrowsException_ShouldThrowException()
2210+
{
2211+
//Arrange
2212+
var specification = new CommentSpecification(x => x.CommentBody.Equals("Comment 0"));
2213+
_commentsRepositoryMock.Setup(r => r.Any(It.IsAny<ISpecification<Comment>>()))
2214+
.Throws(new Exception("DB error"));
2215+
2216+
//Assert
2217+
Assert.Throws<Exception>(() => _commentsService.Any(specification));
2218+
}
2219+
22032220
#endregion
22042221

22052222
#region Any Async function With Specification
@@ -2337,6 +2354,22 @@ public async Task AnyAsync_WithEqualSpecification_WhenCommentDoesNotExists_Shoul
23372354
Assert.False(areAnyComments);
23382355
}
23392356

2357+
/// <summary>
2358+
/// Any async.
2359+
/// When repository throws exception should throw exception.
2360+
/// </summary>
2361+
[Fact]
2362+
public async Task AnyAsync_WhenRepositoryThrowsException_ShouldThrowException()
2363+
{
2364+
//Arrange
2365+
var specification = new CommentSpecification(x => x.CommentBody.Equals("Comment 0"));
2366+
_commentsRepositoryMock.Setup(r => r.AnyAsync(It.IsAny<ISpecification<Comment>>()))
2367+
.ThrowsAsync(new Exception("DB error"));
2368+
2369+
//Assert
2370+
await Assert.ThrowsAsync<Exception>(() => _commentsService.AnyAsync(specification));
2371+
}
2372+
23402373
#endregion
23412374

23422375
#endregion

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Collections.Generic;
1616
using System.Linq;
1717
using System.Threading.Tasks;
18+
using Blog.Data.Specifications.Base;
1819
using Xunit;
1920

2021
namespace Blog.ServicesTests.EntityServices;
@@ -2270,6 +2271,22 @@ public void Any_WithEqualSpecification_WhenMessagesDoesNotExists_ShouldReturnNot
22702271
Assert.False(areAnyMessages);
22712272
}
22722273

2274+
/// <summary>
2275+
/// Any.
2276+
/// When repository throws exception should throw exception.
2277+
/// </summary>
2278+
[Fact]
2279+
public void Any_WhenRepositoryThrowsException_ShouldThrowException()
2280+
{
2281+
//Arrange
2282+
var specification = new MessageSpecification(x => x.Subject.Equals("Test subject0"));
2283+
_messagesRepositoryMock.Setup(r => r.Any(It.IsAny<ISpecification<Message>>()))
2284+
.Throws(new Exception("DB error"));
2285+
2286+
//Assert
2287+
Assert.Throws<Exception>(() => _messagesService.Any(specification));
2288+
}
2289+
22732290
#endregion
22742291

22752292
#region Any Async function With Specification
@@ -2407,6 +2424,22 @@ public async Task AnyAsync_WithEqualSpecification_WhenMessagesDoesNotExists_Shou
24072424
Assert.False(areAnyMessages);
24082425
}
24092426

2427+
/// <summary>
2428+
/// Any async.
2429+
/// When repository throws exception should throw exception.
2430+
/// </summary>
2431+
[Fact]
2432+
public async Task AnyAsync_WhenRepositoryThrowsException_ShouldThrowException()
2433+
{
2434+
//Arrange
2435+
var specification = new MessageSpecification(x => x.Subject.Equals("Test subject0"));
2436+
_messagesRepositoryMock.Setup(r => r.AnyAsync(It.IsAny<ISpecification<Message>>()))
2437+
.ThrowsAsync(new Exception("DB error"));
2438+
2439+
//Assert
2440+
await Assert.ThrowsAsync<Exception>(() => _messagesService.AnyAsync(specification));
2441+
}
2442+
24102443
#endregion
24112444

24122445
#endregion

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,6 +2748,23 @@ public void Any_WithEqualSpecification_WhenPostTagRelationDoesNotExists_ShouldRe
27482748
Assert.False(areAnyPosts);
27492749
}
27502750

2751+
/// <summary>
2752+
/// Any.
2753+
/// When repository throws exception should throw exception.
2754+
/// </summary>
2755+
[Fact]
2756+
public void Any_WhenRepositoryThrowsException_ShouldThrowException()
2757+
{
2758+
//Arrange
2759+
var specification =
2760+
new BaseSpecification<PostsTagsRelations>(x => x.Tag.Title.Equals("Created from ServicesTests 0"));
2761+
_postsTagsRelationsRepositoryMock.Setup(r => r.Any(It.IsAny<ISpecification<PostsTagsRelations>>()))
2762+
.Throws(new Exception("DB error"));
2763+
2764+
//Assert
2765+
Assert.Throws<Exception>(() => _postsTagsRelationsService.Any(specification));
2766+
}
2767+
27512768
#endregion
27522769

27532770
#region Any Async function With Specification
@@ -2886,6 +2903,23 @@ public async Task AnyAsync_WithEqualSpecification_WhenPostTagRelationDoesNotExis
28862903
Assert.False(areAnyPosts);
28872904
}
28882905

2906+
/// <summary>
2907+
/// Any async.
2908+
/// When repository throws exception should throw exception.
2909+
/// </summary>
2910+
[Fact]
2911+
public async Task AnyAsync_WhenRepositoryThrowsException_ShouldThrowException()
2912+
{
2913+
//Arrange
2914+
var specification =
2915+
new BaseSpecification<PostsTagsRelations>(x => x.Tag.Title.Equals("Created from ServicesTests 0"));
2916+
_postsTagsRelationsRepositoryMock.Setup(r => r.AnyAsync(It.IsAny<ISpecification<PostsTagsRelations>>()))
2917+
.ThrowsAsync(new Exception("DB error"));
2918+
2919+
//Assert
2920+
await Assert.ThrowsAsync<Exception>(() => _postsTagsRelationsService.AnyAsync(specification));
2921+
}
2922+
28892923
#endregion
28902924

28912925
#endregion

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections.Generic;
1717
using System.Linq;
1818
using System.Threading.Tasks;
19+
using Blog.Data.Specifications.Base;
1920
using Xunit;
2021

2122
namespace Blog.ServicesTests.EntityServices;
@@ -2252,6 +2253,22 @@ public void Any_WithEqualSpecification_WhenPostDoesNotExists_ShouldReturnNothing
22522253
Assert.False(areAnyPosts);
22532254
}
22542255

2256+
/// <summary>
2257+
/// Any.
2258+
/// When repository throws exception should throw exception.
2259+
/// </summary>
2260+
[Fact]
2261+
public void Any_WhenRepositoryThrowsException_ShouldThrowException()
2262+
{
2263+
//Arrange
2264+
var specification = new PostSpecification(x => x.Title.Equals("Created from ServicesTests 0"));
2265+
_postsRepositoryMock.Setup(r => r.Any(It.IsAny<ISpecification<Post>>()))
2266+
.Throws(new Exception("DB error"));
2267+
2268+
//Assert
2269+
Assert.Throws<Exception>(() => _postsService.Any(specification));
2270+
}
2271+
22552272
#endregion
22562273

22572274
#region Any Async function With Specification
@@ -2389,6 +2406,22 @@ public async Task AnyAsync_WithEqualSpecification_WhenPostDoesNotExists_ShouldRe
23892406
Assert.False(areAnyPosts);
23902407
}
23912408

2409+
/// <summary>
2410+
/// Any async.
2411+
/// When repository throws exception should throw exception.
2412+
/// </summary>
2413+
[Fact]
2414+
public async Task AnyAsync_WhenRepositoryThrowsException_ShouldThrowException()
2415+
{
2416+
//Arrange
2417+
var specification = new PostSpecification(x => x.Title.Equals("Created from ServicesTests 0"));
2418+
_postsRepositoryMock.Setup(r => r.AnyAsync(It.IsAny<ISpecification<Post>>()))
2419+
.ThrowsAsync(new Exception("DB error"));
2420+
2421+
//Assert
2422+
await Assert.ThrowsAsync<Exception>(() => _postsService.AnyAsync(specification));
2423+
}
2424+
23922425
#endregion
23932426

23942427
#endregion

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections.Generic;
1717
using System.Linq;
1818
using System.Threading.Tasks;
19+
using Blog.Data.Specifications.Base;
1920
using Xunit;
2021
using ProfileModel = Blog.Data.Models.Profile;
2122

@@ -2179,6 +2180,23 @@ public void Any_WithEqualSpecification_WhenProfilesDoesNotExists_ShouldReturnNot
21792180
Assert.False(areAnyProfiles);
21802181
}
21812182

2183+
/// <summary>
2184+
/// Any.
2185+
/// When repository throws exception should throw exception.
2186+
/// </summary>
2187+
[Fact]
2188+
public void Any_WhenRepositoryThrowsException_ShouldThrowException()
2189+
{
2190+
//Arrange
2191+
var searchUserId = Guid.Empty.ToString();
2192+
var specification = new ProfileSpecification(x => x.UserId.Equals(searchUserId));
2193+
_profileRepositoryMock.Setup(r => r.Any(It.IsAny<ISpecification<ProfileModel>>()))
2194+
.Throws(new Exception("DB error"));
2195+
2196+
//Assert
2197+
Assert.Throws<Exception>(() => _profileService.Any(specification));
2198+
}
2199+
21822200
#endregion
21832201

21842202
#region Any Async function With Specification
@@ -2290,6 +2308,23 @@ public async Task AnyAsync_WithEqualSpecification_WhenProfilesDoesNotExists_Shou
22902308
Assert.False(areAnyProfiles);
22912309
}
22922310

2311+
/// <summary>
2312+
/// Any async.
2313+
/// When repository throws exception should throw exception.
2314+
/// </summary>
2315+
[Fact]
2316+
public async Task AnyAsync_WhenRepositoryThrowsException_ShouldThrowException()
2317+
{
2318+
//Arrange
2319+
var searchUserId = Guid.Empty.ToString();
2320+
var specification = new ProfileSpecification(x => x.UserId.Equals(searchUserId));
2321+
_profileRepositoryMock.Setup(r => r.AnyAsync(It.IsAny<ISpecification<ProfileModel>>()))
2322+
.ThrowsAsync(new Exception("DB error"));
2323+
2324+
//Assert
2325+
await Assert.ThrowsAsync<Exception>(() => _profileService.AnyAsync(specification));
2326+
}
2327+
22932328
#endregion
22942329

22952330
#endregion

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Collections.Generic;
1616
using System.Linq;
1717
using System.Threading.Tasks;
18+
using Blog.Data.Specifications.Base;
1819
using Xunit;
1920

2021
namespace Blog.ServicesTests.EntityServices;
@@ -2253,6 +2254,22 @@ public void Any_WithEqualSpecification_WhenCommentDoesNotExists_ShouldReturnNoth
22532254
Assert.False(areAnyTags);
22542255
}
22552256

2257+
/// <summary>
2258+
/// Any.
2259+
/// When repository throws exception should throw exception.
2260+
/// </summary>
2261+
[Fact]
2262+
public void Any_WhenRepositoryThrowsException_ShouldThrowException()
2263+
{
2264+
//Arrange
2265+
var specification = new TagSpecification(x => x.Title.Equals("Tag 0"));
2266+
_tagsRepositoryMock.Setup(r => r.Any(It.IsAny<ISpecification<Tag>>()))
2267+
.Throws(new Exception("DB error"));
2268+
2269+
//Assert
2270+
Assert.Throws<Exception>(() => _tagsService.Any(specification));
2271+
}
2272+
22562273
#endregion
22572274

22582275
#region Any Async function With Specification
@@ -2394,6 +2411,22 @@ public async Task AnyAsync_WithEqualSpecification_WhenTagDoesNotExists_ShouldRet
23942411
Assert.False(areAnyTags);
23952412
}
23962413

2414+
/// <summary>
2415+
/// Any async.
2416+
/// When repository throws exception should throw exception.
2417+
/// </summary>
2418+
[Fact]
2419+
public async Task AnyAsync_WhenRepositoryThrowsException_ShouldThrowException()
2420+
{
2421+
//Arrange
2422+
var specification = new TagSpecification(x => x.Title.Equals("Tag 0"));
2423+
_tagsRepositoryMock.Setup(r => r.AnyAsync(specification))
2424+
.ThrowsAsync(new Exception("DB error"));
2425+
2426+
//Assert
2427+
await Assert.ThrowsAsync<Exception>(() => _tagsService.AnyAsync(specification));
2428+
}
2429+
23972430
#endregion
23982431

23992432
#endregion

0 commit comments

Comments
 (0)