コード例 #1
0
        public async Task Edit_WhenStudentIdFromUrlDoesNotMatchWithEntityStudentId_ReturnsBadRequest()
        {
            //Arrange
            var input = GetStudentWithId("111222333");

            //Act
            var result = await _sut.Edit(It.IsAny <string>(), input);

            //Assert
            result.AssertHasValueAndStatusCode(HttpStatusCode.BadRequest);
        }
コード例 #2
0
        public void Edit()
        {
            var result     = controller.Edit(1) as ViewResult;
            var studentIme = _student.GetbyId(1);

            Assert.AreEqual(studentIme.Ime, "Dusan");
        }
コード例 #3
0
        public void EditTest()
        {
            Student v = new Student();

            using (var context = new DataContext(_seed, DBTypeEnum.Memory))
            {
                v.Name = "Ig1N37FOm";
                v.Age  = 20;
                context.Set <Student>().Add(v);
                context.SaveChanges();
            }

            PartialViewResult rv = (PartialViewResult)_controller.Edit(v.ID.ToString());

            Assert.IsInstanceOfType(rv.Model, typeof(StudentVM));

            StudentVM vm = rv.Model as StudentVM;

            vm.Wtm.DC = new DataContext(_seed, DBTypeEnum.Memory);
            v         = new Student();
            v.ID      = vm.Entity.ID;

            v.Name    = "fQFq";
            v.Age     = 92;
            vm.Entity = v;
            vm.FC     = new Dictionary <string, object>();

            vm.FC.Add("Entity.Name", "");
            vm.FC.Add("Entity.Age", "");
            _controller.Edit(vm);

            using (var context = new DataContext(_seed, DBTypeEnum.Memory))
            {
                var data = context.Set <Student>().Find(v.ID);

                Assert.AreEqual(data.Name, "fQFq");
                Assert.AreEqual(data.Age, 92);
                Assert.AreEqual(data.UpdateBy, "user");
                Assert.IsTrue(DateTime.Now.Subtract(data.UpdateTime.Value).Seconds < 10);
            }
        }
コード例 #4
0
        public void Edit_ReturnNotFound_WhenStudentDoesNotExist()
        {
            //Arrange
            StudentService    studentService = Substitute.For <StudentService>();
            StudentController controller     = new StudentController(studentService);

            //Act
            IActionResult actual = controller.Edit(0);

            //Assert
            var expected = new NotFoundResult();

            actual.Should().BeEquivalentTo(expected);
        }
コード例 #5
0
        public void UpdateStudentsTest()
        {
            //Arrange
            StudentController       controller = new StudentController();
            Mock <StudentViewModel> model      = new Mock <StudentViewModel>();

            //Act
            var result =
                controller.Edit(1, model.Object) as RedirectToRouteResult;

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("Index", result.RouteValues["action"]);
            Assert.IsTrue(controller.ModelState.IsValid);
        }
コード例 #6
0
        public void Info_ReturnsNotFound_WhenNonExistingStudentId()
        {
            // Arrange
            int nonExistingStudentId = 0;

            var studentServiceMock = Substitute.For <StudentService>();

            studentServiceMock.GetStudent(nonExistingStudentId).Returns(default(Student));
            var controller = new StudentController(studentServiceMock);

            // Act
            var result = controller.Edit(nonExistingStudentId);

            // Assert
            var viewResult = Assert.IsType <NotFoundResult>(result);
        }
コード例 #7
0
        public void Edit_ReturnsViewResult_WhenModelStateIsInvalid()
        {
            //Arrange
            Student student = new Student {
                Name = "Test student"
            };
            StudentService    studentService = Substitute.For <StudentService>();
            StudentController controller     = new StudentController(studentService);

            //Act
            controller.ModelState.AddModelError("test", "test");  //makes model invalid => if (!ModelState.IsValid)
            IActionResult actual = controller.Edit(student);

            //Assert
            Assert.IsAssignableFrom <ViewResult>(actual);
            actual.Should().BeAssignableTo <ViewResult>();
        }
コード例 #8
0
        public void Edit_Get_ReturnsViewResult_WithStudent()
        {
            // Arrange
            int existingStudentId = 1;

            var studentServiceMock = Substitute.For <StudentService>();

            studentServiceMock.GetStudent(existingStudentId).Returns(new Student());
            var controller = new StudentController(studentServiceMock);

            // Act
            var result = controller.Edit(existingStudentId);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsType <Student>(viewResult.Model);
        }
コード例 #9
0
        public void Edit_RedirectsToStudents_WhenModelStateIsValid()
        {
            //Arrange
            Student student = new Student {
                Name = "Test student"
            };
            StudentService studentService = Substitute.For <StudentService>();

            studentService.GetStudentById(5).Returns(student);
            StudentController controller = new StudentController(studentService);

            //Act
            var actionResult = controller.Edit(student);

            studentService.Received().UpdateStudent(student);

            // Assert
            actionResult.Should().BeOfType <RedirectToActionResult>().Which.ActionName.Should().Be("Students");
        }
コード例 #10
0
        public void Edit_Post_RedirectToStudents()
        {
            // Arrange
            Student studentForEditing = new Student();

            var studentSericeMock = Substitute.For <StudentService>();

            studentSericeMock.UpdateStudent(studentForEditing).Returns(true);
            var controller = new StudentController(studentSericeMock);

            // Act
            var result = controller.Edit(studentForEditing);

            // Assert
            var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Null(redirectToActionResult.ControllerName);
            Assert.Equal("Students", redirectToActionResult.ActionName);
        }
コード例 #11
0
        public async void CanUpdateStudent()
        {
            DbContextOptions <SchoolDbContext> options = new DbContextOptionsBuilder <SchoolDbContext>()
                                                         .UseInMemoryDatabase("StudentUpdateTestDb")
                                                         .Options;

            using (SchoolDbContext context = new SchoolDbContext(options))
            {
                Student student1 = new Student();
                student1.ID             = 3;
                student1.Name           = "Bill Test";
                student1.Level          = Level.Undergraduate;
                student1.CourseID       = 1;
                student1.EnrollmentTerm = EnrollmentTerm.Summer2018;

                StudentController testSC = new StudentController(context);

                var create1 = await testSC.Create(student1);

                student1.ID             = 3;
                student1.Name           = "William Tester";
                student1.Level          = Level.Graduate;
                student1.CourseID       = 2;
                student1.EnrollmentTerm = EnrollmentTerm.Summer2018;

                var edited1 = await testSC.Edit(student1.ID, student1);

                var result1 = await context.Students.FirstOrDefaultAsync(s => s.Name == "Bill Test");

                var result2 = await context.Students.FirstOrDefaultAsync(s => s.Name == "William Tester");

                var result3 = testSC.Details(3);

                //Assert
                Assert.Null(result1);
                Assert.NotNull(result2);
                Assert.Equal("William Tester", student1.Name);
                Assert.Equal(Level.Graduate, student1.Level);
                Assert.Equal(2, student1.CourseID);
                Assert.Equal(EnrollmentTerm.Summer2018, student1.EnrollmentTerm);
            }
        }
コード例 #12
0
        public void EditReturnHttpNotFoundIfStudentNull()
        {
            var identity          = new GenericIdentity("student1");
            var controllerContext = new Mock <ControllerContext>();
            var principal         = new Mock <IPrincipal>();

            principal.SetupGet(x => x.Identity.Name).Returns("student1");
            controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal.Object);
            var mockStudent = new Mock <IStudentRepository>();
            var mock        = new Mock <IRegisterRepository>();
            var mockCourse  = new Mock <ICourseRepository>();
            StudentController controller = new StudentController(mockCourse.Object, mockStudent.Object, mock.Object);

            controller.ControllerContext = controllerContext.Object;
            mockStudent.Setup(c => c.Get(identity.Name)).Returns((Student)null);

            ActionResult result = controller.Edit();

            Assert.AreEqual(result.GetType(), typeof(HttpNotFoundResult));
        }
コード例 #13
0
        public void Edit_ReturnViewResultWithModel_WhenStudentExists()
        {
            //Arrange
            Student student = new Student {
                Name = "Test student"
            };
            StudentService studentService = Substitute.For <StudentService>();

            studentService.GetStudentById(5).Returns(student);
            StudentController controller = new StudentController(studentService);

            //Act
            IActionResult actual = controller.Edit(5);

            //Assert
            actual.Should().BeAssignableTo <ViewResult>();
            ViewResult viewResult = (ViewResult)actual;

            viewResult.Model.Should().BeEquivalentTo(student);
        }
コード例 #14
0
        public void EditPostReturnViewResultIfExceptionOccur()
        {
            var identity          = new GenericIdentity("student1");
            var controllerContext = new Mock <ControllerContext>();
            var principal         = new Mock <IPrincipal>();

            principal.SetupGet(x => x.Identity.Name).Returns("student1");
            controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal.Object);
            var mockStudent = new Mock <IStudentRepository>();
            var mock        = new Mock <IRegisterRepository>();
            var mockCourse  = new Mock <ICourseRepository>();
            StudentController controller = new StudentController(mockCourse.Object, mockStudent.Object, mock.Object);

            controller.ControllerContext = controllerContext.Object;
            mockStudent.Setup(c => c.Get(identity.Name)).Throws <Exception>();

            ActionResult result = controller.Edit(new StudentEditModel());

            Assert.AreEqual(result.GetType(), typeof(ViewResult));
        }
コード例 #15
0
        public void EditPostReturnRedirectIfUpdateOk()
        {
            var identity          = new GenericIdentity("student1");
            var controllerContext = new Mock <ControllerContext>();
            var principal         = new Mock <IPrincipal>();

            principal.SetupGet(x => x.Identity.Name).Returns("student1");
            controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal.Object);
            var mockStudent = new Mock <IStudentRepository>();
            var mock        = new Mock <IRegisterRepository>();
            var mockCourse  = new Mock <ICourseRepository>();
            StudentController controller = new StudentController(mockCourse.Object, mockStudent.Object, mock.Object);

            controller.ControllerContext = controllerContext.Object;
            mockStudent.Setup(c => c.Get(identity.Name)).Returns(new Student());
            mockStudent.Setup(c => c.Update(new Student())).Verifiable();

            ActionResult result = controller.Edit(new StudentEditModel());

            Assert.AreEqual(result.GetType(), typeof(RedirectToRouteResult));
        }
        public void StudentEdit()
        {
            StudentController sk = new StudentController();

            Mock <UnitOfWork>        mockUnit    = new Mock <UnitOfWork>(new Zadatak2Context());
            Mock <StudentRepository> mockStudent = new Mock <StudentRepository>(new Zadatak2Context());

            mockStudent.Setup(x => x.Edit(SeedClass.GetStudentsTest()[1]));
            mockUnit.Setup(x => x.SaveChanges());

            sk.UnitOfWork          = mockUnit.Object;
            sk.UnitOfWork.Studenti = mockStudent.Object;
            ViewResult rezultat = sk.Edit(new StudentModel
            {
                BI      = 1,
                Ime     = "Dragana",
                Prezime = "Vincic",
                Adresa  = "Antifasisticke borbe 22",
                Grad    = "Beograd"
            }) as ViewResult;

            mockStudent.Verify((x => x.Edit(It.IsAny <Student>())), Times.Once());
            mockUnit.Verify((x => x.SaveChanges()), Times.Once());
        }
コード例 #17
0
        public void StudentEditTestValidStudent()
        {
            var db = new TestStudentIspitiContext();
            db.Initialize();
            StudentController controller = new StudentController(db);
            Student temp = new Student { BI = "07_020", Ime = "Jovan", Prezime = "Jovanovic", Adresa = "Dimitrija Tucovica 24", Grad = "Beograd" };

            var result = controller.Edit(temp);

            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            var tempresult = result as RedirectToRouteResult;
            Assert.AreEqual("Index", tempresult.RouteValues["action"]);
        }
コード例 #18
0
        public void StudentEditNullTest()
        {
            /*Test Edit when null is passed*/
            var db = new TestStudentIspitiContext();
            db.Initialize();
            StudentController controller = new StudentController(db);
            string i = null;

            var result = controller.Edit(i);

            Assert.IsInstanceOfType(result, typeof(System.Web.Mvc.HttpStatusCodeResult));
            var httpResult = result as HttpStatusCodeResult;
            Assert.AreEqual(400, httpResult.StatusCode);
        }
コード例 #19
0
        public void StudentEditNotCorrectID()
        {
            var db = new TestStudentIspitiContext();
            db.Initialize();
            StudentController controller = new StudentController(db);
            string i = "99_999";

            var result = controller.Edit(i);

            Assert.IsInstanceOfType(result, typeof(HttpNotFoundResult));
            var httpResult = result as HttpNotFoundResult;
            Assert.AreEqual(404, httpResult.StatusCode);
        }