diff options
| author | transtrike <transtrike@gmail.com> | 2021-03-30 11:46:58 +0300 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-03-30 11:46:58 +0300 |
| commit | d672c515eb760a5351affeb5600640569ed5ee16 (patch) | |
| tree | e851bcfefe95367d6628bb2b716b177f024c46d1 /src | |
| parent | 418c04807eae6220ef609a8c208d67c08cf7f723 (diff) | |
| download | DevHive-d672c515eb760a5351affeb5600640569ed5ee16.tar DevHive-d672c515eb760a5351affeb5600640569ed5ee16.tar.gz DevHive-d672c515eb760a5351affeb5600640569ed5ee16.zip | |
First test implementation and exploration
Diffstat (limited to 'src')
| -rw-r--r-- | src/Services/DevHive.Services.Tests/ProfilePictureService.Tests.cs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/Services/DevHive.Services.Tests/ProfilePictureService.Tests.cs b/src/Services/DevHive.Services.Tests/ProfilePictureService.Tests.cs index 691b8b5..a7a2963 100644 --- a/src/Services/DevHive.Services.Tests/ProfilePictureService.Tests.cs +++ b/src/Services/DevHive.Services.Tests/ProfilePictureService.Tests.cs @@ -1,9 +1,84 @@ using System; +using System.IO; +using System.Threading.Tasks; +using DevHive.Data; +using DevHive.Data.Interfaces; +using DevHive.Data.Models; +using DevHive.Data.Repositories; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.ProfilePicture; +using DevHive.Services.Services; +using Microsoft.AspNetCore.Http; +using Microsoft.EntityFrameworkCore; +using Moq; +using NUnit.Framework; namespace DevHive.Services.Tests { + [TestFixture] public class ProfilePictureServiceTests { + private DevHiveContext _context; + private Mock<IUserRepository> _userRepositoryMock; + private Mock<ProfilePictureRepository> _profilePictureRepository; + private Mock<ICloudService> _cloudService; + private ProfilePictureService _profilePictureService; + [SetUp] + public void Setup() + { + DbContextOptionsBuilder<DevHiveContext> options = new DbContextOptionsBuilder<DevHiveContext>() + .UseInMemoryDatabase("DevHive_UserRepository_Database"); + this._context = new DevHiveContext(options.Options); + this._userRepositoryMock = new Mock<IUserRepository>(); + this._profilePictureRepository = new Mock<ProfilePictureRepository>(this._context); + this._cloudService = new Mock<ICloudService>(); + + this._profilePictureService = new ProfilePictureService( + this._userRepositoryMock.Object, + this._profilePictureRepository.Object, + this._cloudService.Object + ); + } + + [Test] + public async Task InsertProfilePicture_ShouldInsertProfilePicToDatabaseAndUploadToCloud() + { + //Arrange + Guid userId = Guid.NewGuid(); + Mock<IFormFile> fileMock = new(); + + //File mocking setup + var content = "Hello World from a Fake File"; + var fileName = "test.jpg"; + var ms = new MemoryStream(); + var writer = new StreamWriter(ms); + writer.Write(content); + writer.Flush(); + ms.Position = 0; + fileMock.Setup(p => p.FileName).Returns(fileName); + fileMock.Setup(p => p.Length).Returns(ms.Length); + fileMock.Setup(p => p.OpenReadStream()).Returns(ms); + + //User Setup + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(userId)) + .ReturnsAsync(new User() + { + Id = userId + }); + + ProfilePictureServiceModel profilePictureServiceModel = new() + { + UserId = userId, + ProfilePictureFormFile = fileMock.Object + }; + + //Act + string profilePicURL = await this._profilePictureService.InsertProfilePicture(profilePictureServiceModel); + + //Assert + Assert.IsNotEmpty(profilePicURL); + } } } |
