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/PostService.Tests.cs | 104 +++++++++++++++------ 1 file changed, 78 insertions(+), 26 deletions(-) (limited to 'src/Services/DevHive.Services.Tests/PostService.Tests.cs') diff --git a/src/Services/DevHive.Services.Tests/PostService.Tests.cs b/src/Services/DevHive.Services.Tests/PostService.Tests.cs index e51f250..5aea4a8 100644 --- a/src/Services/DevHive.Services.Tests/PostService.Tests.cs +++ b/src/Services/DevHive.Services.Tests/PostService.Tests.cs @@ -53,11 +53,21 @@ namespace DevHive.Services.Tests Id = postId, }; - this._postRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); - this._postRepositoryMock.Setup(p => p.GetPostByCreatorAndTimeCreatedAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(post)); - this._userRepositoryMock.Setup(p => p.DoesUserExistAsync(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(post); + this._postRepositoryMock + .Setup(p => p.AddAsync(It.IsAny())) + .Returns(Task.FromResult(true)); + this._postRepositoryMock + .Setup(p => p.GetPostByCreatorAndTimeCreatedAsync(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(post)); + this._userRepositoryMock + .Setup(p => p.DoesUserExistAsync(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(post); Guid result = await this._postService.CreatePost(createPostServiceModel); @@ -76,9 +86,15 @@ namespace DevHive.Services.Tests Message = MESSAGE, }; - this._postRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); - this._userRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(post); + this._postRepositoryMock + .Setup(p => p.AddAsync(It.IsAny())) + .Returns(Task.FromResult(false)); + this._userRepositoryMock + .Setup(p => p.DoesUserExistAsync(It.IsAny())) + .Returns(Task.FromResult(true)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(post); Guid result = await this._postService.CreatePost(createPostServiceModel); @@ -119,9 +135,15 @@ namespace DevHive.Services.Tests Id = creatorId, }; - this._postRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(post)); - this._userRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(readPostServiceModel); + this._postRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(post)); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(user)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readPostServiceModel); ReadPostServiceModel result = await this._postService.GetPostById(Guid.NewGuid()); @@ -140,7 +162,9 @@ namespace DevHive.Services.Tests Creator = creator }; - this._postRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(post)); + this._postRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(post)); Exception ex = Assert.ThrowsAsync(() => this._postService.GetPostById(Guid.NewGuid()), "GetPostById does not throw exception when the user does not exist"); @@ -157,8 +181,12 @@ namespace DevHive.Services.Tests Id = creatorId, }; - this._postRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); - this._userRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); + this._postRepositoryMock + .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._postService.GetPostById(Guid.NewGuid())); @@ -183,10 +211,18 @@ namespace DevHive.Services.Tests Files = new List() }; - this._postRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); - this._postRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - this._postRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(post)); - this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(post); + this._postRepositoryMock + .Setup(p => p.DoesPostExist(It.IsAny())) + .Returns(Task.FromResult(true)); + this._postRepositoryMock + .Setup(p => p.EditAsync(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(true)); + this._postRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(post)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(post); Guid result = await this._postService.UpdatePost(updatePostServiceModel); @@ -207,9 +243,15 @@ namespace DevHive.Services.Tests Files = new List() }; - this._postRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); - this._postRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); - this._mapperMock.Setup(p => p.Map(It.IsAny())).Returns(post); + this._postRepositoryMock + .Setup(p => p.DoesPostExist(It.IsAny())) + .Returns(Task.FromResult(true)); + this._postRepositoryMock + .Setup(p => p.EditAsync(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(false)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(post); Guid result = await this._postService.UpdatePost(updatePostServiceModel); @@ -224,7 +266,9 @@ namespace DevHive.Services.Tests { }; - this._postRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(false)); + this._postRepositoryMock + .Setup(p => p.DoesPostExist(It.IsAny())) + .Returns(Task.FromResult(false)); Exception ex = Assert.ThrowsAsync(() => this._postService.UpdatePost(updatePostServiceModel)); @@ -241,9 +285,15 @@ namespace DevHive.Services.Tests Guid id = Guid.NewGuid(); Post post = new Post(); - this._postRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); - this._postRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(post)); - this._postRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); + this._postRepositoryMock + .Setup(p => p.DoesPostExist(It.IsAny())) + .Returns(Task.FromResult(true)); + this._postRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(post)); + this._postRepositoryMock + .Setup(p => p.DeleteAsync(It.IsAny())) + .Returns(Task.FromResult(shouldPass)); bool result = await this._postService.DeletePost(id); @@ -256,7 +306,9 @@ namespace DevHive.Services.Tests string exceptionMessage = "Post does not exist!"; Guid id = Guid.NewGuid(); - this._postRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(false)); + this._postRepositoryMock + .Setup(p => p.DoesPostExist(It.IsAny())) + .Returns(Task.FromResult(false)); Exception ex = Assert.ThrowsAsync(() => this._postService.DeletePost(id)); -- cgit v1.2.3