public async Task CreateAsync_ShouldThrowException_WithInvalidData(string fullName, int age, string aboGroup, string rhesusFactor, int hospitalId, int neededBloodBanks, string ward) { var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var seeder = new Seeder(); await seeder.SeedUsersAsync(context); await seeder.SeedBloodTypesAsync(context); var userManager = this.GetUserManagerMock(context); var patientService = new PatientService(context, userManager.Object); var patientServiceModel = new PatientServiceModel { FullName = fullName, Age = age, BloodType = new BloodDonationSystem.Services.Models.BloodTypeServiceModel { ABOGroupName = aboGroup, RhesusFactor = rhesusFactor, }, HospitalId = hospitalId, NeededBloodBanks = neededBloodBanks, Ward = ward, UserId = context.Users.First().Id, }; await Assert.ThrowsAsync <ArgumentNullException>(async() => { await patientService.CreateAsync(patientServiceModel); }); }
public async Task <IActionResult> Create([FromBody] PatientRequest request) { try { await patientService.CreateAsync(request); return(Ok()); } catch (Exception e) { return(BadRequest(e.Message)); } }
public async Task CreateAsync_WithCorectData_ShouldReturnCorectResult(string fullName, int age, string aboGroup, string rhesusFactor, int hospitalId, int neededBloodBanks, string ward) { var errorMessage = "PatientService CreateAsync method does not return properly "; var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var seeder = new Seeder(); await seeder.SeedUsersAsync(context); await seeder.SeedBloodTypesAsync(context); var userManager = this.GetUserManagerMock(context); var patientService = new PatientService(context, userManager.Object); var patientServiceModel = new PatientServiceModel { FullName = fullName, Age = age, BloodType = new BloodDonationSystem.Services.Models.BloodTypeServiceModel { ABOGroupName = aboGroup, RhesusFactor = rhesusFactor, }, HospitalId = hospitalId, NeededBloodBanks = neededBloodBanks, Ward = ward, UserId = context.Users.First().Id, }; var result = await patientService.CreateAsync(patientServiceModel); var actualResult = context.Patients.FirstOrDefault(); var expectedResult = patientServiceModel; Assert.True(result); Assert.True(actualResult.FullName == expectedResult.FullName, errorMessage + "FullName"); Assert.True(actualResult.Age == expectedResult.Age, errorMessage + "Age"); Assert.True(actualResult.BloodType.ABOGroupName.ToString() == expectedResult.BloodType.ABOGroupName.ToString(), errorMessage + "ABOGroup"); Assert.True(actualResult.BloodType.RhesusFactor.ToString() == expectedResult.BloodType.RhesusFactor.ToString(), errorMessage + "RhesusFactor"); Assert.True(actualResult.HospitalId == expectedResult.HospitalId, errorMessage + "HospitalId"); Assert.True(actualResult.NeededBloodBanks == expectedResult.NeededBloodBanks, errorMessage + "NeededBloodBanks"); Assert.True(actualResult.Ward == expectedResult.Ward, errorMessage + "Ward"); Assert.True(actualResult.UserId == expectedResult.UserId, errorMessage + "UserId"); }
public async Task CreateAsync_PatientValidationSucceed_CreatesPatient() { // Arrange var patient = new PatientUpdateModel(); var expected = new Patient(); var streetService = new Mock <IStreetService>(); streetService.Setup(x => x.ValidateAsync(patient)); var patientDAL = new Mock <IPatientDAL>(); patientDAL.Setup(x => x.InsertAsync(patient)).ReturnsAsync(expected); var patientService = new PatientService(patientDAL.Object, streetService.Object); // Act var result = await patientService.CreateAsync(patient); // Assert result.Should().Be(expected); }
public async Task CreateAsync_PatientValidationFailed_ThrowsError() { // Arrange var fixture = new Fixture(); var patient = new PatientUpdateModel(); var expected = fixture.Create <string>(); var streetService = new Mock <IStreetService>(); streetService .Setup(x => x.ValidateAsync(patient)) .Throws(new InvalidOperationException(expected)); var patientDAL = new Mock <IPatientDAL>(); var patientService = new PatientService(patientDAL.Object, streetService.Object); var action = new Func <Task>(() => patientService.CreateAsync(patient)); // Assert await action.Should().ThrowAsync <InvalidOperationException>().WithMessage(expected); patientDAL.Verify(x => x.InsertAsync(patient), Times.Never); }