public async Task CreateAbsence_WithCorrectData_ShouldSuccessfullyCreateOrder() { string errorMessagePrefix = "AbsenceService CreateAbsenceAsync() method does not work properly."; var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.absenceService = new AbsenceService(context); var student = new Student { Id = "test", FullName = "Test", Absences = new List <Absence>(), }; await context.Students.AddAsync(student); await context.SaveChangesAsync(); AbsenceServiceModel testAbsence = new AbsenceServiceModel { StudentId = student.Id, Student = student.To <StudentServiceModel>(), }; bool actualResult = await this.absenceService.CreateAbsenceAsync(testAbsence); var updatedStudent = context.Students.First(); var expectedAbsencesCount = 1; Assert.True(actualResult, errorMessagePrefix); Assert.Equal(updatedStudent.Absences.Count, expectedAbsencesCount); }
public async Task CreateAbsence_WithNonExistentStudent_ShouldReturnArgumentNullException() { string errorMessagePrefix = "AbsenceService CreateAbsenceAsync() method does not work properly."; var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.absenceService = new AbsenceService(context); AbsenceServiceModel testAbsence = new AbsenceServiceModel(); await Assert.ThrowsAsync <ArgumentNullException>(async() => await this.absenceService.CreateAbsenceAsync(testAbsence)); }
public async Task <bool> CreateAbsenceAsync(AbsenceServiceModel model) { var absence = Mapper.Map <Absence>(model); var student = await this.context.Students.Where(x => !x.IsDeleted).FirstOrDefaultAsync(s => s.Id == absence.StudentId); if (student == null) { throw new ArgumentNullException(nameof(student)); } student.Absences.Add(absence); this.context.Students.Update(student); await this.context.Absences.AddAsync(absence); int result = await this.context.SaveChangesAsync(); return(result > 0); }