public async Task CreatePOST_ValidModelState_ReturnRedirectIndexActionAndInsertStudents() { var student = new Student { Name = "Unit test name", Address = "Unit test adres", SchoolId = 1 }; using (var context = new UnitTestDBContext(_contextOptions)) { var studentController = new StudentsController(context); //StudentsController base class'dan geliyor IActionResult actionResult = await studentController.Create(student); var redirectToActionResult = Assert.IsAssignableFrom <RedirectToActionResult>(actionResult); Assert.Equal("Index", redirectToActionResult.ActionName); } //EF Core Entity Track ettiğinden dolayı Context'i her defada new()'leyerek kullanıyorum. //Her istekte new()'leyip test sonucunu garantiliyorum. var studentDbEntity = await Context.Students.FirstOrDefaultAsync(p => p.Id == student.Id); Assert.NotNull(studentDbEntity); Assert.Equal <int>(student.Id, studentDbEntity.Id); }
public async Task DeleteSchool_ExitsSchoolId_DeletedAllStudents(int schoolId) { using (var context = new UnitTestDBContext(_contextOptions)) { var school = await context.Schools.FindAsync(schoolId); Assert.NotNull(school); context.Schools.Remove(school); await context.SaveChangesAsync(); } using (var context = new UnitTestDBContext(_contextOptions)) { var students = await context.Students.Where(p => p.SchoolId == schoolId).ToListAsync(); Assert.Empty(students); } }
public Repository(UnitTestDBContext context) { _context = context; _dbSet = context.Set <TEntity>(); }
public StudentsController(UnitTestDBContext context) { _context = context; }