public async Task CreateMentorAsync() { //Arrange int mentorExpectedId = 5; var successExistingAccount = new Account() { Id = 1 }; var assignedExistingAccount = new Account() { Id = 2, Role = UserRole.Mentor }; _accountServiceMock.Setup(x => x.GetAccountCredentialsByIdAsync(1)) .ReturnsAsync(successExistingAccount); _accountServiceMock.Setup(x => x.GetAccountCredentialsByIdAsync(2)) .ReturnsAsync(assignedExistingAccount); var mentorRepositoryMock = new Mock <IMentorRepository>(); mentorRepositoryMock.Setup(x => x.Add(It.IsAny <Mentor>())) .Callback <Mentor>(x => x.Id = mentorExpectedId); _unitOfWorkMock.Setup(x => x.MentorRepository).Returns(mentorRepositoryMock.Object); var mentorService = new MentorService( _accountServiceMock.Object, _unitOfWorkMock.Object, _mapper, _notificationServiceMock.Object); //Act var nonExistingIdResult = await mentorService.CreateMentorAsync(0); var successResult = await mentorService.CreateMentorAsync(1); var alreadyAssignedResult = await mentorService.CreateMentorAsync(2); //Assert Assert.Equal(ErrorCode.NotFound, nonExistingIdResult.Error.Code); Assert.NotNull(successResult.Data); Assert.Equal(mentorExpectedId, successResult.Data.Id); Assert.Equal(ErrorCode.ValidationError, alreadyAssignedResult.Error.Code); }
public MentorController(MentorService MentorService) { _mentorService = MentorService; }
public async Task UpdateMentorAsync() { //Arrange string usedEmail = "*****@*****.**"; var nonExistingUpdateMentorDto = new UpdateMentorDto(); var successUpdateMentorDto = new UpdateMentorDto() { Email = "*****@*****.**", FirstName = "updateTest", LastName = "updateTest" }; var successMentor = new Mentor() { Id = 1, AccountId = 1, Account = new Account() { Id = 1, Email = "*****@*****.**" } }; var alreadyExistingEmailUpdateMentorDto = new UpdateMentorDto() { Email = usedEmail, FirstName = "updateTest", LastName = "updateTest" }; var alreadyExistingEmailMentor = new Mentor() { Id = 2, AccountId = 2, Account = new Account() { Id = 2, Email = usedEmail } }; _accountServiceMock.Setup(x => x.IsEmailChangableToAsync( (long)successMentor.AccountId, successUpdateMentorDto.Email)) .ReturnsAsync(true); _accountServiceMock.Setup(x => x.IsEmailChangableToAsync( (long)alreadyExistingEmailMentor.AccountId, usedEmail)) .ReturnsAsync(false); var mentorRepositoryMock = new Mock <IMentorRepository>(); mentorRepositoryMock.Setup(x => x.GetByIdAsync(1)) .ReturnsAsync(successMentor); mentorRepositoryMock.Setup(x => x.GetByIdAsync(2)) .ReturnsAsync(alreadyExistingEmailMentor); _unitOfWorkMock.Setup(x => x.MentorRepository).Returns(mentorRepositoryMock.Object); var mentorService = new MentorService( _accountServiceMock.Object, _unitOfWorkMock.Object, _mapper, _notificationServiceMock.Object); //Act var nonExistingIdResult = await mentorService .UpdateMentorAsync(0, nonExistingUpdateMentorDto); var successResult = await mentorService .UpdateMentorAsync(1, successUpdateMentorDto); var alreadyExistingEmailResult = await mentorService .UpdateMentorAsync(2, alreadyExistingEmailUpdateMentorDto); //Assert Assert.Equal(ErrorCode.NotFound, nonExistingIdResult.Error.Code); Assert.NotNull(successResult.Data); Assert.Equal(successUpdateMentorDto.Email, successResult.Data.Email); Assert.Equal(successUpdateMentorDto.FirstName, successResult.Data.FirstName); Assert.Equal(successUpdateMentorDto.LastName, successResult.Data.LastName); Assert.Equal(ErrorCode.ValidationError, alreadyExistingEmailResult.Error.Code); }