From a2dde73b37ad311213a6787fb5aeb5cb4103968c Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 14 Mar 2021 11:23:35 +0200 Subject: Updated code style of mock setups in Web layer tests --- .../DevHive.Web.Tests/CommentController.Tests.cs | 120 +++++++++++++++------ 1 file changed, 90 insertions(+), 30 deletions(-) (limited to 'src/Web/DevHive.Web.Tests/CommentController.Tests.cs') diff --git a/src/Web/DevHive.Web.Tests/CommentController.Tests.cs b/src/Web/DevHive.Web.Tests/CommentController.Tests.cs index 3b7f0a9..4e1715c 100644 --- a/src/Web/DevHive.Web.Tests/CommentController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/CommentController.Tests.cs @@ -47,10 +47,18 @@ namespace DevHive.Web.Tests Message = MESSAGE }; - this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(createCommentServiceModel); - this._commentServiceMock.Setup(p => p.AddComment(It.IsAny())).Returns(Task.FromResult(id)); - this._jwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this._commentServiceMock.Setup(p => p.ValidateJwtForCreating(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(createCommentServiceModel); + this._commentServiceMock + .Setup(p => p.AddComment(It.IsAny())) + .Returns(Task.FromResult(id)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForCreating(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(true)); IActionResult result = this._commentController.AddComment(Guid.NewGuid(), createCommentWebModel, null).Result; @@ -81,10 +89,18 @@ namespace DevHive.Web.Tests string errorMessage = $"Could not create comment!"; - this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(createCommentServiceModel); - this._commentServiceMock.Setup(p => p.AddComment(It.IsAny())).Returns(Task.FromResult(Guid.Empty)); - this._jwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this._commentServiceMock.Setup(p => p.ValidateJwtForCreating(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(createCommentServiceModel); + this._commentServiceMock + .Setup(p => p.AddComment(It.IsAny())) + .Returns(Task.FromResult(Guid.Empty)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForCreating(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(true)); IActionResult result = this._commentController.AddComment(Guid.NewGuid(), createCommentWebModel, null).Result; @@ -104,8 +120,12 @@ namespace DevHive.Web.Tests Message = MESSAGE }; - this._jwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this._commentServiceMock.Setup(p => p.ValidateJwtForCreating(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForCreating(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(false)); IActionResult result = this._commentController.AddComment(Guid.NewGuid(), createCommentWebModel, null).Result; @@ -127,9 +147,15 @@ namespace DevHive.Web.Tests Message = MESSAGE }; - this._jwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this._commentServiceMock.Setup(p => p.GetCommentById(It.IsAny())).Returns(Task.FromResult(readCommentServiceModel)); - this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(readCommentWebModel); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.GetCommentById(It.IsAny())) + .Returns(Task.FromResult(readCommentServiceModel)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readCommentWebModel); IActionResult result = this._commentController.GetCommentById(id).Result; @@ -156,10 +182,18 @@ namespace DevHive.Web.Tests NewMessage = MESSAGE }; - this._commentServiceMock.Setup(p => p.UpdateComment(It.IsAny())).Returns(Task.FromResult(id)); - this._jwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this._commentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateCommentServiceModel); + this._commentServiceMock + .Setup(p => p.UpdateComment(It.IsAny())) + .Returns(Task.FromResult(id)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(true)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(updateCommentServiceModel); IActionResult result = this._commentController.UpdateComment(Guid.Empty, updateCommentWebModel, null).Result; @@ -185,10 +219,18 @@ namespace DevHive.Web.Tests NewMessage = MESSAGE }; - this._commentServiceMock.Setup(p => p.UpdateComment(It.IsAny())).Returns(Task.FromResult(Guid.Empty)); - this._jwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this._commentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateCommentServiceModel); + this._commentServiceMock + .Setup(p => p.UpdateComment(It.IsAny())) + .Returns(Task.FromResult(Guid.Empty)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(true)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(updateCommentServiceModel); IActionResult result = this._commentController.UpdateComment(Guid.Empty, updateCommentWebModel, null).Result; Assert.IsInstanceOf(result); @@ -207,7 +249,9 @@ namespace DevHive.Web.Tests NewMessage = MESSAGE }; - this._jwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(false); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(false); // this.CommentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); IActionResult result = this._commentController.UpdateComment(Guid.Empty, updateCommentWebModel, null).Result; @@ -222,9 +266,15 @@ namespace DevHive.Web.Tests { Guid id = Guid.NewGuid(); - this._commentServiceMock.Setup(p => p.DeleteComment(It.IsAny())).Returns(Task.FromResult(true)); - this._jwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this._commentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + this._commentServiceMock + .Setup(p => p.DeleteComment(It.IsAny())) + .Returns(Task.FromResult(true)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(true)); IActionResult result = this._commentController.DeleteComment(id, null).Result; @@ -237,9 +287,15 @@ namespace DevHive.Web.Tests string message = "Could not delete Comment"; Guid id = Guid.NewGuid(); - this._commentServiceMock.Setup(p => p.DeleteComment(It.IsAny())).Returns(Task.FromResult(false)); - this._jwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this._commentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + this._commentServiceMock + .Setup(p => p.DeleteComment(It.IsAny())) + .Returns(Task.FromResult(false)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(true)); IActionResult result = this._commentController.DeleteComment(id, null).Result; @@ -254,8 +310,12 @@ namespace DevHive.Web.Tests [Test] public void DeleteComment_ReturnsUnauthorizedResult_WhenUserIsNotAuthorized() { - this._jwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this._commentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(false)); IActionResult result = this._commentController.DeleteComment(Guid.Empty, null).Result; -- cgit v1.2.3