From 5372fa5e7cd1a3d97a61052cd87f2105c65b143a Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 14 Mar 2021 11:39:23 +0200 Subject: Updated code style of mock setups in Service layer tests --- .../DevHive.Services.Tests/CommentService.Tests.cs | 104 +++++++++++++++------ 1 file changed, 78 insertions(+), 26 deletions(-) (limited to 'src/Services/DevHive.Services.Tests/CommentService.Tests.cs') diff --git a/src/Services/DevHive.Services.Tests/CommentService.Tests.cs b/src/Services/DevHive.Services.Tests/CommentService.Tests.cs index a73cf90..729e943 100644 --- a/src/Services/DevHive.Services.Tests/CommentService.Tests.cs +++ b/src/Services/DevHive.Services.Tests/CommentService.Tests.cs @@ -48,11 +48,21 @@ namespace DevHive.Services.Tests Id = id, }; - _ = this._commentRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); - _ = this._commentRepositoryMock.Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(comment)); - _ = this._postRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); - _ = this._userRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(creator)); - _ = this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + this._commentRepositoryMock + .Setup(p => p.AddAsync(It.IsAny())) + .Returns(Task.FromResult(true)); + this._commentRepositoryMock + .Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(comment)); + this._postRepositoryMock + .Setup(p => p.DoesPostExist(It.IsAny())) + .Returns(Task.FromResult(true)); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(creator)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(comment); Guid result = await this._commentService.AddComment(createCommentServiceModel); @@ -71,9 +81,15 @@ namespace DevHive.Services.Tests Message = MESSAGE, }; - _ = this._commentRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); - _ = this._postRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); - _ = this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + this._commentRepositoryMock + .Setup(p => p.AddAsync(It.IsAny())) + .Returns(Task.FromResult(false)); + this._postRepositoryMock + .Setup(p => p.DoesPostExist(It.IsAny())) + .Returns(Task.FromResult(true)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(comment); Guid result = await this._commentService.AddComment(createCommentServiceModel); @@ -116,9 +132,15 @@ namespace DevHive.Services.Tests Id = creatorId, }; - _ = this._commentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); - _ = this._userRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - _ = this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(commentServiceModel); + this._commentRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(comment)); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(user)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(commentServiceModel); ReadCommentServiceModel result = await this._commentService.GetCommentById(Guid.NewGuid()); @@ -137,7 +159,9 @@ namespace DevHive.Services.Tests Creator = creator }; - _ = this._commentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); + this._commentRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(comment)); Exception ex = Assert.ThrowsAsync(() => this._commentService.GetCommentById(Guid.NewGuid()), "GetCommentById does not throw exception when the user does not exist"); @@ -154,8 +178,12 @@ namespace DevHive.Services.Tests Id = creatorId, }; - _ = this._commentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); - _ = this._userRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); + this._commentRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(null)); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(user)); Exception ex = Assert.ThrowsAsync(() => this._commentService.GetCommentById(Guid.NewGuid())); @@ -179,10 +207,18 @@ namespace DevHive.Services.Tests NewMessage = MESSAGE }; - _ = this._commentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); - _ = this._commentRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - _ = this._commentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); - _ = this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + this._commentRepositoryMock + .Setup(p => p.DoesCommentExist(It.IsAny())) + .Returns(Task.FromResult(true)); + this._commentRepositoryMock + .Setup(p => p.EditAsync(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(true)); + this._commentRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(comment)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(comment); Guid result = await this._commentService.UpdateComment(updateCommentServiceModel); @@ -202,9 +238,15 @@ namespace DevHive.Services.Tests NewMessage = MESSAGE }; - _ = this._commentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); - _ = this._commentRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); - _ = this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + this._commentRepositoryMock + .Setup(p => p.DoesCommentExist(It.IsAny())) + .Returns(Task.FromResult(true)); + this._commentRepositoryMock + .Setup(p => p.EditAsync(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(false)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(comment); Guid result = await this._commentService.UpdateComment(updateCommentServiceModel); @@ -219,7 +261,9 @@ namespace DevHive.Services.Tests { }; - _ = this._commentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(false)); + this._commentRepositoryMock + .Setup(p => p.DoesCommentExist(It.IsAny())) + .Returns(Task.FromResult(false)); Exception ex = Assert.ThrowsAsync(() => this._commentService.UpdateComment(updateCommentServiceModel)); @@ -236,9 +280,15 @@ namespace DevHive.Services.Tests Guid id = new(); Comment comment = new(); - _ = this._commentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); - _ = this._commentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); - _ = this._commentRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); + this._commentRepositoryMock + .Setup(p => p.DoesCommentExist(It.IsAny())) + .Returns(Task.FromResult(true)); + this._commentRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(comment)); + this._commentRepositoryMock + .Setup(p => p.DeleteAsync(It.IsAny())) + .Returns(Task.FromResult(shouldPass)); bool result = await this._commentService.DeleteComment(id); @@ -251,7 +301,9 @@ namespace DevHive.Services.Tests string exceptionMessage = "Comment does not exist!"; Guid id = new(); - _ = this._commentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(false)); + this._commentRepositoryMock + .Setup(p => p.DoesCommentExist(It.IsAny())) + .Returns(Task.FromResult(false)); Exception ex = Assert.ThrowsAsync(() => this._commentService.DeleteComment(id)); -- cgit v1.3.1-26-gb76a