aboutsummaryrefslogtreecommitdiff
path: root/src/Services/DevHive.Services.Tests/PostService.Tests.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Services/DevHive.Services.Tests/PostService.Tests.cs')
-rw-r--r--src/Services/DevHive.Services.Tests/PostService.Tests.cs168
1 files changed, 108 insertions, 60 deletions
diff --git a/src/Services/DevHive.Services.Tests/PostService.Tests.cs b/src/Services/DevHive.Services.Tests/PostService.Tests.cs
index 137b8d1..058485e 100644
--- a/src/Services/DevHive.Services.Tests/PostService.Tests.cs
+++ b/src/Services/DevHive.Services.Tests/PostService.Tests.cs
@@ -17,23 +17,23 @@ namespace DevHive.Services.Tests
public class PostServiceTests
{
private const string MESSAGE = "Gosho Trapov";
- private Mock<ICloudService> CloudServiceMock { get; set; }
- private Mock<IPostRepository> PostRepositoryMock { get; set; }
- private Mock<ICommentRepository> CommentRepositoryMock { get; set; }
- private Mock<IUserRepository> UserRepositoryMock { get; set; }
- private Mock<IMapper> MapperMock { get; set; }
- private PostService PostService { get; set; }
+ private Mock<ICloudService> _cloudServiceMock;
+ private Mock<IPostRepository> _postRepositoryMock;
+ private Mock<ICommentRepository> _commentRepositoryMock;
+ private Mock<IUserRepository> _userRepositoryMock;
+ private Mock<IMapper> _mapperMock;
+ private PostService _postService;
#region SetUps
[SetUp]
public void Setup()
{
- this.PostRepositoryMock = new Mock<IPostRepository>();
- this.CloudServiceMock = new Mock<ICloudService>();
- this.UserRepositoryMock = new Mock<IUserRepository>();
- this.CommentRepositoryMock = new Mock<ICommentRepository>();
- this.MapperMock = new Mock<IMapper>();
- this.PostService = new PostService(this.CloudServiceMock.Object, this.UserRepositoryMock.Object, this.PostRepositoryMock.Object, this.CommentRepositoryMock.Object, this.MapperMock.Object);
+ this._postRepositoryMock = new Mock<IPostRepository>();
+ this._cloudServiceMock = new Mock<ICloudService>();
+ this._userRepositoryMock = new Mock<IUserRepository>();
+ this._commentRepositoryMock = new Mock<ICommentRepository>();
+ this._mapperMock = new Mock<IMapper>();
+ this._postService = new PostService(this._cloudServiceMock.Object, this._userRepositoryMock.Object, this._postRepositoryMock.Object, this._commentRepositoryMock.Object, this._mapperMock.Object);
}
#endregion
@@ -53,13 +53,23 @@ namespace DevHive.Services.Tests
Id = postId,
};
- this.PostRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Post>())).Returns(Task.FromResult(true));
- this.PostRepositoryMock.Setup(p => p.GetPostByCreatorAndTimeCreatedAsync(It.IsAny<Guid>(), It.IsAny<DateTime>())).Returns(Task.FromResult(post));
- this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(creator));
- this.MapperMock.Setup(p => p.Map<Post>(It.IsAny<CreatePostServiceModel>())).Returns(post);
-
- Guid result = await this.PostService.CreatePost(createPostServiceModel);
+ this._postRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Post>()))
+ .ReturnsAsync(true);
+ this._postRepositoryMock
+ .Setup(p => p.GetPostByCreatorAndTimeCreatedAsync(It.IsAny<Guid>(), It.IsAny<DateTime>()))
+ .ReturnsAsync(post);
+ this._userRepositoryMock
+ .Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(creator);
+ this._mapperMock
+ .Setup(p => p.Map<Post>(It.IsAny<CreatePostServiceModel>()))
+ .Returns(post);
+
+ Guid result = await this._postService.CreatePost(createPostServiceModel);
Assert.AreEqual(postId, result, "CreatePost does not return the correct id");
}
@@ -76,11 +86,17 @@ namespace DevHive.Services.Tests
Message = MESSAGE,
};
- this.PostRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Post>())).Returns(Task.FromResult(false));
- this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.MapperMock.Setup(p => p.Map<Post>(It.IsAny<CreatePostServiceModel>())).Returns(post);
+ this._postRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Post>()))
+ .ReturnsAsync(false);
+ this._userRepositoryMock
+ .Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._mapperMock
+ .Setup(p => p.Map<Post>(It.IsAny<CreatePostServiceModel>()))
+ .Returns(post);
- Guid result = await this.PostService.CreatePost(createPostServiceModel);
+ Guid result = await this._postService.CreatePost(createPostServiceModel);
Assert.AreEqual(Guid.Empty, result, "CreatePost does not return empty id");
}
@@ -92,12 +108,8 @@ namespace DevHive.Services.Tests
CreatePostServiceModel createPostServiceModel = new CreatePostServiceModel
{
};
- Post post = new Post
- {
- Message = MESSAGE,
- };
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.CreatePost(createPostServiceModel), "CreatePost does not throw excpeion when the user does not exist");
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._postService.CreatePost(createPostServiceModel), "CreatePost does not throw excpeion when the user does not exist");
Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Excapetion message is not correct");
}
@@ -107,7 +119,7 @@ namespace DevHive.Services.Tests
[Test]
public async Task GetPostById_ReturnsThePost_WhenItExists()
{
- Guid creatorId = new Guid();
+ Guid creatorId = Guid.NewGuid();
User creator = new User { Id = creatorId };
Post post = new Post
{
@@ -123,11 +135,17 @@ namespace DevHive.Services.Tests
Id = creatorId,
};
- this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(post));
- this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));
- this.MapperMock.Setup(p => p.Map<ReadPostServiceModel>(It.IsAny<Post>())).Returns(readPostServiceModel);
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(post);
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
+ this._mapperMock
+ .Setup(p => p.Map<ReadPostServiceModel>(It.IsAny<Post>()))
+ .Returns(readPostServiceModel);
- ReadPostServiceModel result = await this.PostService.GetPostById(new Guid());
+ ReadPostServiceModel result = await this._postService.GetPostById(Guid.NewGuid());
Assert.AreEqual(MESSAGE, result.Message);
}
@@ -136,7 +154,7 @@ namespace DevHive.Services.Tests
public void GetPostById_ThorwsException_WhenTheUserDoesNotExist()
{
const string EXCEPTION_MESSAGE = "The user does not exist!";
- Guid creatorId = new Guid();
+ Guid creatorId = Guid.NewGuid();
User creator = new User { Id = creatorId };
Post post = new Post
{
@@ -144,9 +162,11 @@ namespace DevHive.Services.Tests
Creator = creator
};
- this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(post));
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(post);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.GetPostById(new Guid()), "GetPostById does not throw exception when the user does not exist");
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._postService.GetPostById(Guid.NewGuid()), "GetPostById does not throw exception when the user does not exist");
Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message);
}
@@ -155,16 +175,20 @@ namespace DevHive.Services.Tests
public void GetPostById_ThrowsException_WhenCommentDoesNotExist()
{
string exceptionMessage = "The post does not exist!";
- Guid creatorId = new Guid();
+ Guid creatorId = Guid.NewGuid();
User user = new User
{
Id = creatorId,
};
- this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Post>(null));
- this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Post>(null));
+ this._userRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(user);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.GetPostById(new Guid()));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._postService.GetPostById(Guid.NewGuid()));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -187,12 +211,20 @@ namespace DevHive.Services.Tests
Files = new List<IFormFile>()
};
- this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.PostRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Post>())).Returns(Task.FromResult(true));
- this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(post));
- this.MapperMock.Setup(p => p.Map<Post>(It.IsAny<UpdatePostServiceModel>())).Returns(post);
-
- Guid result = await this.PostService.UpdatePost(updatePostServiceModel);
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._postRepositoryMock
+ .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Post>()))
+ .ReturnsAsync(true);
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(post);
+ this._mapperMock
+ .Setup(p => p.Map<Post>(It.IsAny<UpdatePostServiceModel>()))
+ .Returns(post);
+
+ Guid result = await this._postService.UpdatePost(updatePostServiceModel);
Assert.AreEqual(updatePostServiceModel.PostId, result);
}
@@ -211,11 +243,17 @@ namespace DevHive.Services.Tests
Files = new List<IFormFile>()
};
- this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.PostRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Post>())).Returns(Task.FromResult(false));
- this.MapperMock.Setup(p => p.Map<Post>(It.IsAny<UpdatePostServiceModel>())).Returns(post);
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._postRepositoryMock
+ .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Post>()))
+ .ReturnsAsync(false);
+ this._mapperMock
+ .Setup(p => p.Map<Post>(It.IsAny<UpdatePostServiceModel>()))
+ .Returns(post);
- Guid result = await this.PostService.UpdatePost(updatePostServiceModel);
+ Guid result = await this._postService.UpdatePost(updatePostServiceModel);
Assert.AreEqual(Guid.Empty, result);
}
@@ -228,9 +266,11 @@ namespace DevHive.Services.Tests
{
};
- this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.UpdatePost(updatePostServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._postService.UpdatePost(updatePostServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -242,14 +282,20 @@ namespace DevHive.Services.Tests
[TestCase(false)]
public async Task Deletepost_ShouldReturnIfDeletionIsSuccessfull_WhenPostExists(bool shouldPass)
{
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
Post post = new Post();
- this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(post));
- this.PostRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<Post>())).Returns(Task.FromResult(shouldPass));
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._postRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(post);
+ this._postRepositoryMock
+ .Setup(p => p.DeleteAsync(It.IsAny<Post>()))
+ .ReturnsAsync(shouldPass);
- bool result = await this.PostService.DeletePost(id);
+ bool result = await this._postService.DeletePost(id);
Assert.AreEqual(shouldPass, result);
}
@@ -258,11 +304,13 @@ namespace DevHive.Services.Tests
public void DeletePost_ThrowsException_WhenPostDoesNotExist()
{
string exceptionMessage = "Post does not exist!";
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
- this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._postRepositoryMock
+ .Setup(p => p.DoesPostExist(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.DeletePost(id));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._postService.DeletePost(id));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}