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);
        }
示例#2
0
        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);
            }
        }
示例#3
0
 public Repository(UnitTestDBContext context)
 {
     _context = context;
     _dbSet   = context.Set <TEntity>();
 }
示例#4
0
 public StudentsController(UnitTestDBContext context)
 {
     _context = context;
 }