From 0c5f69a8abf861334196341ca9cc82753305602f Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 13 Mar 2021 15:04:34 +0200 Subject: Added tests for rating repository --- .../DevHive.Data.Tests/RatingRepository.Tests.cs | 188 +++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs (limited to 'src/Data/DevHive.Data.Tests') diff --git a/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs new file mode 100644 index 0000000..17616fe --- /dev/null +++ b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs @@ -0,0 +1,188 @@ +using DevHive.Data.Repositories; +using Microsoft.EntityFrameworkCore; +using NUnit.Framework; +using System.Threading.Tasks; +using DevHive.Data.Models; +using System.Linq; +using System; +using System.Collections.Generic; + +namespace DevHive.Data.Tests +{ + [TestFixture] + public class RatingRepositoryTests + { + private DevHiveContext Context { get; set; } + private RatingRepository RatingRepository { get; set; } + + #region Setups + [SetUp] + public void Setup() + { + var optionsBuilder = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); + + this.Context = new DevHiveContext(optionsBuilder.Options); + this.RatingRepository = new RatingRepository(this.Context, null); + } + + [TearDown] + public void TearDown() + { + this.Context.Database.EnsureDeleted(); + } + #endregion + + #region GetById + [Test] + public async Task GetByIdAsync_ReturnsTheCorrectRating_IfItExists() + { + Guid ratingId = Guid.NewGuid(); + await AddDummyRating(ratingId); + + Rating ratingResult = await this.RatingRepository.GetByIdAsync(ratingId); + + Assert.AreEqual(ratingResult.Id, ratingId); + } + + [Test] + public async Task GetByIdAsync_ReturnsNull_IfRatingDoesNotExist() + { + Rating ratingResult = await this.RatingRepository.GetByIdAsync(Guid.NewGuid()); + + Assert.IsNull(ratingResult); + } + #endregion + + #region GetByPostId + [Test] + public async Task GetRatingsByPostId_ReturnsFilledListOfRatings_WhenTheyExist() + { + Guid postId = Guid.NewGuid(); + await AddDummyPost(postId); + await AddDummyRating(Guid.NewGuid(), postId); + await AddDummyRating(Guid.NewGuid(), postId); + + List result = await this.RatingRepository.GetRatingsByPostId(postId); + + Assert.IsNotEmpty(result); + } + + [Test] + public async Task GetRatingsByPostId_ReturnsEmptyList_WhenThereAreNoRatings() + { + List result = await this.RatingRepository.GetRatingsByPostId(Guid.NewGuid()); + + Assert.IsEmpty(result); + } + #endregion + + #region GetByUserAndPostId + [Test] + public async Task GetRatingByUserAndPostId_ReturnsRating_WhenItExists() + { + Guid ratingId = Guid.NewGuid(); + Guid postId = Guid.NewGuid(); + Guid userId = Guid.NewGuid(); + await AddDummyPost(postId); + await AddDummyUser(userId); + await AddDummyRating(ratingId, postId, userId); + + Rating result = await this.RatingRepository.GetRatingByUserAndPostId(userId, postId); + + Assert.AreEqual(result.Id, ratingId); + } + + [Test] + public async Task GetRatingByUserAndPostId_ReturnsNull_WhenRatingDoesNotExist() + { + Rating result = await this.RatingRepository.GetRatingByUserAndPostId(Guid.NewGuid(), Guid.NewGuid()); + + Assert.IsNull(result); + } + #endregion + + #region UserRatedPost + [Test] + public async Task UserRatedPost_ReturnsTrue_WhenUserHasRatedPost() + { + Guid postId = Guid.NewGuid(); + Guid userId = Guid.NewGuid(); + await AddDummyPost(postId); + await AddDummyUser(userId); + await AddDummyRating(Guid.NewGuid(), postId, userId); + + bool result = await this.RatingRepository.UserRatedPost(userId, postId); + + Assert.IsTrue(result); + } + + [Test] + public async Task UserRatedPost_ReturnsFalse_WhenUserHasNotRatedPost() + { + bool result = await this.RatingRepository.UserRatedPost(Guid.NewGuid(), Guid.NewGuid()); + + Assert.IsFalse(result); + } + #endregion + + #region DoesRatingExist + [Test] + public async Task DoesRatingExist_ReturnsTrue_WhenItExists() + { + Guid ratingId = Guid.NewGuid(); + await AddDummyRating(ratingId); + + bool result = await this.RatingRepository.DoesRatingExist(ratingId); + + Assert.IsTrue(result); + } + + [Test] + public async Task DoesRatingExist_ReturnsFalse_WhenRatingDoesNotExist() + { + bool result = await this.RatingRepository.DoesRatingExist(Guid.NewGuid()); + + Assert.IsFalse(result); + } + #endregion + + #region HelperMethods + private async Task AddDummyRating(Guid ratingId, Guid postId = default(Guid), Guid userId = default(Guid)) + { + Rating rating = new Rating + { + Id = ratingId, + Post = this.Context.Posts.FirstOrDefault(x => x.Id == postId), + User = this.Context.Users.FirstOrDefault(x => x.Id == userId) + }; + + await this.Context.Rating.AddAsync(rating); + await this.Context.SaveChangesAsync(); + } + + private async Task AddDummyPost(Guid postId) + { + Post post = new Post() + { + Id = postId, + Message = "Never gonna give you up" + }; + + await this.Context.Posts.AddAsync(post); + await this.Context.SaveChangesAsync(); + } + + private async Task AddDummyUser(Guid userId) + { + User user = new User() + { + Id = userId + }; + + await this.Context.Users.AddAsync(user); + await this.Context.SaveChangesAsync(); + } + #endregion + } +} -- cgit v1.2.3 From 76f71bb8cab45922f3e4999253a1567a0e4af3db Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 14 Mar 2021 21:39:37 +0200 Subject: Updated rating tests to follow our current code style standards --- .../DevHive.Data.Tests/RatingRepository.Tests.cs | 46 ++--- .../DevHive.Services.Tests/RatingService.Tests.cs | 210 ++++++++++++++------- .../DevHive.Web.Tests/RatingController.Tests.cs | 118 ++++++++---- 3 files changed, 247 insertions(+), 127 deletions(-) (limited to 'src/Data/DevHive.Data.Tests') diff --git a/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs index 17616fe..2da90d9 100644 --- a/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs @@ -12,8 +12,8 @@ namespace DevHive.Data.Tests [TestFixture] public class RatingRepositoryTests { - private DevHiveContext Context { get; set; } - private RatingRepository RatingRepository { get; set; } + private DevHiveContext _context; + private RatingRepository _ratingRepository; #region Setups [SetUp] @@ -22,14 +22,14 @@ namespace DevHive.Data.Tests var optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); - this.Context = new DevHiveContext(optionsBuilder.Options); - this.RatingRepository = new RatingRepository(this.Context, null); + this._context = new DevHiveContext(optionsBuilder.Options); + this._ratingRepository = new RatingRepository(this._context, null); } [TearDown] public void TearDown() { - this.Context.Database.EnsureDeleted(); + this._context.Database.EnsureDeleted(); } #endregion @@ -40,7 +40,7 @@ namespace DevHive.Data.Tests Guid ratingId = Guid.NewGuid(); await AddDummyRating(ratingId); - Rating ratingResult = await this.RatingRepository.GetByIdAsync(ratingId); + Rating ratingResult = await this._ratingRepository.GetByIdAsync(ratingId); Assert.AreEqual(ratingResult.Id, ratingId); } @@ -48,7 +48,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetByIdAsync_ReturnsNull_IfRatingDoesNotExist() { - Rating ratingResult = await this.RatingRepository.GetByIdAsync(Guid.NewGuid()); + Rating ratingResult = await this._ratingRepository.GetByIdAsync(Guid.NewGuid()); Assert.IsNull(ratingResult); } @@ -63,7 +63,7 @@ namespace DevHive.Data.Tests await AddDummyRating(Guid.NewGuid(), postId); await AddDummyRating(Guid.NewGuid(), postId); - List result = await this.RatingRepository.GetRatingsByPostId(postId); + List result = await this._ratingRepository.GetRatingsByPostId(postId); Assert.IsNotEmpty(result); } @@ -71,7 +71,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetRatingsByPostId_ReturnsEmptyList_WhenThereAreNoRatings() { - List result = await this.RatingRepository.GetRatingsByPostId(Guid.NewGuid()); + List result = await this._ratingRepository.GetRatingsByPostId(Guid.NewGuid()); Assert.IsEmpty(result); } @@ -88,7 +88,7 @@ namespace DevHive.Data.Tests await AddDummyUser(userId); await AddDummyRating(ratingId, postId, userId); - Rating result = await this.RatingRepository.GetRatingByUserAndPostId(userId, postId); + Rating result = await this._ratingRepository.GetRatingByUserAndPostId(userId, postId); Assert.AreEqual(result.Id, ratingId); } @@ -96,7 +96,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetRatingByUserAndPostId_ReturnsNull_WhenRatingDoesNotExist() { - Rating result = await this.RatingRepository.GetRatingByUserAndPostId(Guid.NewGuid(), Guid.NewGuid()); + Rating result = await this._ratingRepository.GetRatingByUserAndPostId(Guid.NewGuid(), Guid.NewGuid()); Assert.IsNull(result); } @@ -112,7 +112,7 @@ namespace DevHive.Data.Tests await AddDummyUser(userId); await AddDummyRating(Guid.NewGuid(), postId, userId); - bool result = await this.RatingRepository.UserRatedPost(userId, postId); + bool result = await this._ratingRepository.UserRatedPost(userId, postId); Assert.IsTrue(result); } @@ -120,7 +120,7 @@ namespace DevHive.Data.Tests [Test] public async Task UserRatedPost_ReturnsFalse_WhenUserHasNotRatedPost() { - bool result = await this.RatingRepository.UserRatedPost(Guid.NewGuid(), Guid.NewGuid()); + bool result = await this._ratingRepository.UserRatedPost(Guid.NewGuid(), Guid.NewGuid()); Assert.IsFalse(result); } @@ -133,7 +133,7 @@ namespace DevHive.Data.Tests Guid ratingId = Guid.NewGuid(); await AddDummyRating(ratingId); - bool result = await this.RatingRepository.DoesRatingExist(ratingId); + bool result = await this._ratingRepository.DoesRatingExist(ratingId); Assert.IsTrue(result); } @@ -141,7 +141,7 @@ namespace DevHive.Data.Tests [Test] public async Task DoesRatingExist_ReturnsFalse_WhenRatingDoesNotExist() { - bool result = await this.RatingRepository.DoesRatingExist(Guid.NewGuid()); + bool result = await this._ratingRepository.DoesRatingExist(Guid.NewGuid()); Assert.IsFalse(result); } @@ -153,12 +153,12 @@ namespace DevHive.Data.Tests Rating rating = new Rating { Id = ratingId, - Post = this.Context.Posts.FirstOrDefault(x => x.Id == postId), - User = this.Context.Users.FirstOrDefault(x => x.Id == userId) + Post = this._context.Posts.FirstOrDefault(x => x.Id == postId), + User = this._context.Users.FirstOrDefault(x => x.Id == userId) }; - await this.Context.Rating.AddAsync(rating); - await this.Context.SaveChangesAsync(); + await this._context.Rating.AddAsync(rating); + await this._context.SaveChangesAsync(); } private async Task AddDummyPost(Guid postId) @@ -169,8 +169,8 @@ namespace DevHive.Data.Tests Message = "Never gonna give you up" }; - await this.Context.Posts.AddAsync(post); - await this.Context.SaveChangesAsync(); + await this._context.Posts.AddAsync(post); + await this._context.SaveChangesAsync(); } private async Task AddDummyUser(Guid userId) @@ -180,8 +180,8 @@ namespace DevHive.Data.Tests Id = userId }; - await this.Context.Users.AddAsync(user); - await this.Context.SaveChangesAsync(); + await this._context.Users.AddAsync(user); + await this._context.SaveChangesAsync(); } #endregion } diff --git a/src/Services/DevHive.Services.Tests/RatingService.Tests.cs b/src/Services/DevHive.Services.Tests/RatingService.Tests.cs index 89c1a2e..5f84530 100644 --- a/src/Services/DevHive.Services.Tests/RatingService.Tests.cs +++ b/src/Services/DevHive.Services.Tests/RatingService.Tests.cs @@ -13,21 +13,21 @@ namespace DevHive.Services.Tests [TestFixture] public class RatingServiceTests { - private Mock PostRepositoryMock { get; set; } - private Mock RatingRepositoryMock { get; set; } - private Mock UserRepositoryMock { get; set; } - private Mock MapperMock { get; set; } - private RatingService RatingService { get; set; } + private Mock _postRepositoryMock; + private Mock _ratingRepositoryMock; + private Mock _userRepositoryMock; + private Mock _mapperMock; + private RatingService _ratingService; #region SetUps [SetUp] public void SetUp() { - this.PostRepositoryMock = new Mock(); - this.RatingRepositoryMock = new Mock(); - this.UserRepositoryMock = new Mock(); - this.MapperMock = new Mock(); - this.RatingService = new RatingService(this.PostRepositoryMock.Object, this.RatingRepositoryMock.Object, this.UserRepositoryMock.Object, this.MapperMock.Object); + this._postRepositoryMock = new Mock(); + this._ratingRepositoryMock = new Mock(); + this._userRepositoryMock = new Mock(); + this._mapperMock = new Mock(); + this._ratingService = new RatingService(this._postRepositoryMock.Object, this._ratingRepositoryMock.Object, this._userRepositoryMock.Object, this._mapperMock.Object); } #endregion @@ -59,15 +59,29 @@ namespace DevHive.Services.Tests Id = postId }; - this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(post)); - this.RatingRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(rating); - - Guid result = await this.RatingService.RatePost(createRatingServiceModel); + this._postRepositoryMock + .Setup(p => p.DoesPostExist(It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) + .ReturnsAsync(false); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(user); + this._postRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(post); + this._ratingRepositoryMock + .Setup(p => p.AddAsync(It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .ReturnsAsync(rating); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(rating); + + Guid result = await this._ratingService.RatePost(createRatingServiceModel); Assert.AreEqual(id, result); } @@ -99,14 +113,26 @@ namespace DevHive.Services.Tests Id = postId }; - this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(post)); - this.RatingRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(rating); - - Guid result = await this.RatingService.RatePost(createRatingServiceModel); + this._postRepositoryMock + .Setup(p => p.DoesPostExist(It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) + .ReturnsAsync(false); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(user); + this._postRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(post); + this._ratingRepositoryMock + .Setup(p => p.AddAsync(It.IsAny())) + .ReturnsAsync(false); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(rating); + + Guid result = await this._ratingService.RatePost(createRatingServiceModel); Assert.AreEqual(result, Guid.Empty); } @@ -134,10 +160,14 @@ namespace DevHive.Services.Tests IsLike = isLike }; - this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(rating)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); + this._ratingRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(rating); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readRatingServiceModel); - ReadRatingServiceModel result = await this.RatingService.GetRatingById(id); + ReadRatingServiceModel result = await this._ratingService.GetRatingById(id); Assert.AreEqual(isLike, result.IsLike); } @@ -146,9 +176,11 @@ namespace DevHive.Services.Tests public void GetRatingById_ThrowsException_WhenRatingDoesNotExist() { string exceptionMessage = "The rating does not exist"; - this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); + this._ratingRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(null)); - Exception ex = Assert.ThrowsAsync(() => this.RatingService.GetRatingById(Guid.Empty)); + Exception ex = Assert.ThrowsAsync(() => this._ratingService.GetRatingById(Guid.Empty)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } @@ -174,10 +206,14 @@ namespace DevHive.Services.Tests IsLike = isLike }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .ReturnsAsync(rating); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readRatingServiceModel); - ReadRatingServiceModel result = await this.RatingService.GetRatingByPostAndUser(user.Id, Guid.Empty); + ReadRatingServiceModel result = await this._ratingService.GetRatingByPostAndUser(user.Id, Guid.Empty); Assert.AreEqual(isLike, result.IsLike); } @@ -186,9 +222,11 @@ namespace DevHive.Services.Tests public void GetRatingByPostAndUser_ThrowsException_WhenRatingDoesNotExist() { string exceptionMessage = "The rating does not exist"; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(null)); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(null)); - Exception ex = Assert.ThrowsAsync(() => this.RatingService.GetRatingById(Guid.Empty)); + Exception ex = Assert.ThrowsAsync(() => this._ratingService.GetRatingById(Guid.Empty)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } @@ -221,13 +259,23 @@ namespace DevHive.Services.Tests IsLike = isLike }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); - - ReadRatingServiceModel result = await this.RatingService.UpdateRating(updateRatingServiceModel); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .ReturnsAsync(rating); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(user); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.EditAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(true); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readRatingServiceModel); + + ReadRatingServiceModel result = await this._ratingService.UpdateRating(updateRatingServiceModel); Assert.AreEqual(result, readRatingServiceModel); } @@ -258,12 +306,20 @@ namespace DevHive.Services.Tests IsLike = isLike }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); - - ReadRatingServiceModel result = await this.RatingService.UpdateRating(updateRatingServiceModel); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .ReturnsAsync(rating); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(user); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.EditAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(false); + + ReadRatingServiceModel result = await this._ratingService.UpdateRating(updateRatingServiceModel); Assert.IsNull(result); } @@ -278,9 +334,11 @@ namespace DevHive.Services.Tests IsLike = true }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(null)); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(null)); - Exception ex = Assert.ThrowsAsync(() => this.RatingService.UpdateRating(updateRatingServiceModel)); + Exception ex = Assert.ThrowsAsync(() => this._ratingService.UpdateRating(updateRatingServiceModel)); Assert.AreEqual(ex.Message, exceptionMessage); } @@ -307,11 +365,17 @@ namespace DevHive.Services.Tests User = user }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .ReturnsAsync(rating); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(user); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) + .ReturnsAsync(false); - Exception ex = Assert.ThrowsAsync(() => this.RatingService.UpdateRating(updateRatingServiceModel)); + Exception ex = Assert.ThrowsAsync(() => this._ratingService.UpdateRating(updateRatingServiceModel)); Assert.AreEqual(ex.Message, exceptionMessage); } @@ -327,11 +391,17 @@ namespace DevHive.Services.Tests Id = ratingId }; - this.RatingRepositoryMock.Setup(p => p.DoesRatingExist(It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); - this.RatingRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(true)); + this._ratingRepositoryMock + .Setup(p => p.DoesRatingExist(It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(null)); + this._ratingRepositoryMock + .Setup(p => p.DeleteAsync(It.IsAny())) + .ReturnsAsync(true); - bool result = await this.RatingService.DeleteRating(ratingId); + bool result = await this._ratingService.DeleteRating(ratingId); Assert.IsTrue(result); } @@ -345,11 +415,17 @@ namespace DevHive.Services.Tests Id = ratingId }; - this.RatingRepositoryMock.Setup(p => p.DoesRatingExist(It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); - this.RatingRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(false)); + this._ratingRepositoryMock + .Setup(p => p.DoesRatingExist(It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(null)); + this._ratingRepositoryMock + .Setup(p => p.DeleteAsync(It.IsAny())) + .ReturnsAsync(false); - bool result = await this.RatingService.DeleteRating(ratingId); + bool result = await this._ratingService.DeleteRating(ratingId); Assert.IsFalse(result); } @@ -359,9 +435,11 @@ namespace DevHive.Services.Tests { string exceptionMessage = "Rating does not exist!"; - this.RatingRepositoryMock.Setup(p => p.DoesRatingExist(It.IsAny())).Returns(Task.FromResult(false)); + this._ratingRepositoryMock + .Setup(p => p.DoesRatingExist(It.IsAny())) + .ReturnsAsync(false); - Exception ex = Assert.ThrowsAsync(() => this.RatingService.DeleteRating(Guid.Empty)); + Exception ex = Assert.ThrowsAsync(() => this._ratingService.DeleteRating(Guid.Empty)); Assert.AreEqual(ex.Message, exceptionMessage); } diff --git a/src/Web/DevHive.Web.Tests/RatingController.Tests.cs b/src/Web/DevHive.Web.Tests/RatingController.Tests.cs index dd8954f..c7340a6 100644 --- a/src/Web/DevHive.Web.Tests/RatingController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/RatingController.Tests.cs @@ -16,18 +16,18 @@ namespace DevHive.Web.Tests [TestFixture] public class RatingControllerTests { - private Mock RatingServiceMock { get; set; } - private Mock MapperMock { get; set; } - private Mock JwtServiceMock { get; set; } - private RatingController RatingController { get; set; } + private Mock _ratingServiceMock; + private Mock _mapperMock; + private Mock _jwtServiceMock; + private RatingController _ratingController; [SetUp] public void SetUp() { - this.RatingServiceMock = new Mock(); - this.MapperMock = new Mock(); - this.JwtServiceMock = new Mock(); - this.RatingController = new RatingController(this.RatingServiceMock.Object, this.MapperMock.Object, this.JwtServiceMock.Object); + this._ratingServiceMock = new Mock(); + this._mapperMock = new Mock(); + this._jwtServiceMock = new Mock(); + this._ratingController = new RatingController(this._ratingServiceMock.Object, this._mapperMock.Object, this._jwtServiceMock.Object); } #region Create @@ -47,11 +47,17 @@ namespace DevHive.Web.Tests }; Guid ratingId = Guid.NewGuid(); - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createRatingServiceModel); - this.RatingServiceMock.Setup(p => p.RatePost(It.IsAny())).Returns(Task.FromResult(ratingId)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(createRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.RatePost(It.IsAny())) + .ReturnsAsync(ratingId); - IActionResult result = this.RatingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; + IActionResult result = this._ratingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; Assert.IsInstanceOf(result); @@ -82,11 +88,17 @@ namespace DevHive.Web.Tests }; Guid ratingId = Guid.NewGuid(); - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createRatingServiceModel); - this.RatingServiceMock.Setup(p => p.RatePost(It.IsAny())).Returns(Task.FromResult(Guid.Empty)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(createRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.RatePost(It.IsAny())) + .ReturnsAsync(Guid.Empty); - IActionResult result = this.RatingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; + IActionResult result = this._ratingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; Assert.IsInstanceOf(result); } @@ -114,10 +126,14 @@ namespace DevHive.Web.Tests IsLike = true }; - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); - this.RatingServiceMock.Setup(p => p.GetRatingById(It.IsAny())).Returns(Task.FromResult(readRatingServiceModel)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.GetRatingById(It.IsAny())) + .ReturnsAsync(readRatingServiceModel); - IActionResult result = this.RatingController.GetRatingById(id).Result; + IActionResult result = this._ratingController.GetRatingById(id).Result; Assert.IsInstanceOf(result); } @@ -143,10 +159,14 @@ namespace DevHive.Web.Tests IsLike = true }; - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); - this.RatingServiceMock.Setup(p => p.GetRatingByPostAndUser(It.IsAny(), It.IsAny())).Returns(Task.FromResult(readRatingServiceModel)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.GetRatingByPostAndUser(It.IsAny(), It.IsAny())) + .ReturnsAsync(readRatingServiceModel); - IActionResult result = this.RatingController.GetRatingByUserAndPost(userId, postId).Result; + IActionResult result = this._ratingController.GetRatingByUserAndPost(userId, postId).Result; Assert.IsInstanceOf(result); } @@ -186,12 +206,20 @@ namespace DevHive.Web.Tests IsLike = true }; - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateRatingServiceModel); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingWebModel); - this.RatingServiceMock.Setup(p => p.UpdateRating(It.IsAny())).Returns(Task.FromResult(readRatingServiceModel)); - - IActionResult result = this.RatingController.UpdateRating(userId, postId, updateRatingWebModel, String.Empty).Result; + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(updateRatingServiceModel); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readRatingWebModel); + this._ratingServiceMock + .Setup(p => p.UpdateRating(It.IsAny())) + .ReturnsAsync(readRatingServiceModel); + + IActionResult result = this._ratingController.UpdateRating(userId, postId, updateRatingWebModel, String.Empty).Result; Assert.IsInstanceOf(result); } @@ -210,11 +238,17 @@ namespace DevHive.Web.Tests IsLike = true }; - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateRatingServiceModel); - this.RatingServiceMock.Setup(p => p.UpdateRating(It.IsAny())).Returns(Task.FromResult(null)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(updateRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.UpdateRating(It.IsAny())) + .Returns(Task.FromResult(null)); - IActionResult result = this.RatingController.UpdateRating(Guid.Empty, Guid.Empty, updateRatingWebModel, String.Empty).Result; + IActionResult result = this._ratingController.UpdateRating(Guid.Empty, Guid.Empty, updateRatingWebModel, String.Empty).Result; Assert.IsInstanceOf(result); } @@ -226,10 +260,14 @@ namespace DevHive.Web.Tests { Guid id = Guid.NewGuid(); - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this.RatingServiceMock.Setup(p => p.DeleteRating(It.IsAny())).Returns(Task.FromResult(true)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._ratingServiceMock + .Setup(p => p.DeleteRating(It.IsAny())) + .ReturnsAsync(true); - IActionResult result = this.RatingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; + IActionResult result = this._ratingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; Assert.IsInstanceOf(result); } @@ -240,10 +278,14 @@ namespace DevHive.Web.Tests string message = "Could not delete Rating"; Guid id = Guid.NewGuid(); - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this.RatingServiceMock.Setup(p => p.DeleteRating(It.IsAny())).Returns(Task.FromResult(false)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._ratingServiceMock + .Setup(p => p.DeleteRating(It.IsAny())) + .ReturnsAsync(false); - IActionResult result = this.RatingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; + IActionResult result = this._ratingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; Assert.IsInstanceOf(result); -- cgit v1.2.3