public void Controller_Student_Create_Post_Id_Is_Null_Or_Empty_Should_Return_Back_For_Edit() { // Arrange StudentController controller = new StudentController(); StudentModel dataNull = new StudentModel(); StudentModel dataEmpty = new StudentModel(); // Make data.Id = null dataNull.Id = null; // Make data.Id empty dataEmpty.Id = ""; var context = CreateMoqSetupForCookie(); controller.ControllerContext = new ControllerContext(context, new RouteData(), controller); // Act var resultNull = (ViewResult)controller.Create(dataNull); var resultEmpty = (ViewResult)controller.Create(dataEmpty); // Assert Assert.IsNotNull(resultNull, TestContext.TestName); Assert.IsNotNull(resultEmpty, TestContext.TestName); }
public void StudentControllerCanDelete() { DbContextOptions <EnrollmentDbContext> options = new DbContextOptionsBuilder <EnrollmentDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; using (EnrollmentDbContext context = new EnrollmentDbContext(options)) { Course course = new Course(); course.ID = 3; course.ClassName = ClassName.Python; course.Instructor = "Miss Snake"; course.StartingDate = new DateTime(2012, 12, 12); course.EndDate = new DateTime(2012, 12, 25); Student student = new Student(); student.Name = "Mario"; student.CourseId = 3; student.Passing = true; student.HoursOfSleep = 5; CourseController cc = new CourseController(context); var y = cc.Create(course); StudentController sc = new StudentController(context); var x = sc.Create(student); var z = sc.Delete(1); var results = context.Student.Where(a => a.Name == "Mario"); Assert.Equal(0, results.Count()); } }
public void StudentControllerCanCreate() { DbContextOptions <Lab13StudentEnrollmentDbContext> options = new DbContextOptionsBuilder <Lab13StudentEnrollmentDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; using (Lab13StudentEnrollmentDbContext context = new Lab13StudentEnrollmentDbContext(options)) { // Arrange Course course = new Course(); course.Name = "DotNet"; course.Description = "Embrace the flood"; CourseController cc = new CourseController(context); var x = cc.Create(course); Student student = new Student(); student.Name = "Someone"; student.CourseID = 1; // Act StudentController sc = new StudentController(context); var y = sc.Create(student); var results = context.Students.Where(a => a.Name == "Someone"); // Assert Assert.Equal(1, results.Count()); } }
public async Task Create_Student_ReturnsException_When_Same_Email() { // Arrange //Student student = new Student() { Name = "Bob", Email = "*****@*****.**", Id = 1, PhoneNumber = "380999999999" }; Student fakeStudent = new Student() { Name = "Bob", Email = "*****@*****.**", Id = 2, PhoneNumber = "380999999999" }; //StudentController model = new StudentController(null); var studentServiceMock = Substitute.For <StudentService>(); studentServiceMock.GetAllStudents().Returns(this.GetStudentsList()); var controller = new StudentController(studentServiceMock); // Act var a = controller.Create(fakeStudent); // Assert var actualView = Assert.IsType <ViewResult>(a); var actualModel = actualView.ViewData.ModelState; var actualViewName = actualView.ViewName; actualViewName.Should().BeEquivalentTo("Edit"); //ModelStateEntry expected = new ModelStateEntry(); actualModel.Values.Single().Errors.Single().ErrorMessage.Should().BeEquivalentTo("User with the same email is already registrated"); actualModel.IsValid.Should().BeFalse(); }
private void btn_add_student_Click_1(object sender, EventArgs e) { StudentName = tb_Student_Name.Text; StudentSurname = tb_Student_Surname.Text; StudentAge = Convert.ToInt32(tb_Student_Age.Text); StudentLogin = tb_student_login.Text; StudentPassword = tb_student_password.Text; StudentController.Create(); }
public async Task CanCreateStudent() { var options = new DbContextOptionsBuilder <StudentContext>() .UseInMemoryDatabase(databaseName: "testing221") .Options; using (var context = new StudentContext(options)) { var SController = new StudentController(context); Course c = new Course() { Department = "CPTS", Description = "Awesome", Instructor = "OFallon", Level = 121 }; await context.Course.AddAsync(c); await context.SaveChangesAsync(); var course = await context.Course.FirstOrDefaultAsync(x => x.Department == "CPTS"); Student s1 = new Student() { LastName = "Scott", FirstName = "Was Here", Enrolled = course.ID }; Student s2 = new Student() { LastName = "Steve", FirstName = "Was Not Here", Enrolled = course.ID }; await context.Student.AddAsync(s1); await context.Student.AddAsync(s2); await context.SaveChangesAsync(); var list = await context.Student.ToListAsync(); Assert.Equal(2, list.Count); StudentViewModel newStudent = new StudentViewModel { FirstName = "Jason", LastName = "Bourne", CourseDepartment = "CPTS", CourseNumber = 121 }; //Act await SController.Create(newStudent); //Assert var numStudents = await context.Student.CountAsync(); Assert.Equal(3, numStudents); var result = await context.Student.SingleOrDefaultAsync(x => x.FirstName == "Jason"); Assert.NotNull(result); } }
//FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2005-09-01") public void Create() { // Arrange StudentController controller = new StudentController(); // Act ViewResult result = controller.Create() as ViewResult; // Assert Assert.IsNotNull(result); }
public void Create_Get_ReturnsViewResult() { // Arrange var controller = new StudentController(null); //Act var result = controller.Create(); // Assert var viewResult = Assert.IsType <ViewResult>(result); }
public void Create_ReturnsBadRequest_WhenStudentParameterIsNull() { //Arrange Student student = null; StudentService studentService = Substitute.For <StudentService>(); StudentController controller = new StudentController(studentService); //Act IActionResult actual = controller.Create(student); //Assert actual.Should().BeAssignableTo <BadRequestResult>(); }
public void StudentCreateTestValidStudent() { 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.Create(temp);// as RedirectToRouteResult; Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult)); var tempresult = result as RedirectToRouteResult; Assert.AreEqual("Index", tempresult.RouteValues["action"]); }
public void CreateTest() { PartialViewResult rv = (PartialViewResult)_controller.Create(); Assert.IsInstanceOfType(rv.Model, typeof(StudentVM)); StudentVM vm = rv.Model as StudentVM; Student v = new Student(); v.Name = "zKk"; vm.Entity = v; _controller.Create(vm); using (var context = new DataContext(_seed, DBTypeEnum.Memory)) { var data = context.Set <Student>().FirstOrDefault(); Assert.AreEqual(data.Name, "zKk"); Assert.AreEqual(data.CreateBy, "user"); Assert.IsTrue(DateTime.Now.Subtract(data.CreateTime.Value).Seconds < 10); } }
public async void CanDeleteStudent() { DbContextOptions <SchoolDbContext> options = new DbContextOptionsBuilder <SchoolDbContext>() .UseInMemoryDatabase("DeleteStudentDB").Options; using (SchoolDbContext context = new SchoolDbContext(options)) { // arrange Student student1 = new Student(); student1.ID = 4; student1.Name = "Bill Test"; student1.Level = Level.Undergraduate; student1.CourseID = 1; student1.EnrollmentTerm = EnrollmentTerm.Summer2018; Student student2 = new Student(); student2.ID = 5; student2.Name = "Sally Testing"; student2.Level = Level.Graduate; student2.CourseID = 3; student2.EnrollmentTerm = EnrollmentTerm.Spring2019; // act StudentController testSC = new StudentController(context); await testSC.Create(student1); await testSC.Create(student2); await testSC.DeleteConfirmed(4); var results1 = context.Students.Where(s => s.Name == "Bill Test"); var results2 = context.Students.Where(s => s.Name == "Sally Testing"); // assert Assert.Equal(0, results1.Count()); Assert.Equal(1, results2.Count()); } }
public void CreateTest() { Student student = new Student() { firstName = "Jan", email = "*****@*****.**" }; var context = GetInMemoryDBMetData(); StudentController s = new StudentController(context); var result = Assert.IsType <RedirectToActionResult>(s.Create(student)); Assert.True(context.students.Any(s => s.studentNumber == student.studentNumber)); }
public void Controller_Student_Create_Default_Should_Pass() { // Arrange var controller = new StudentController(); var context = CreateMoqSetupForCookie(); controller.ControllerContext = new ControllerContext(context, new RouteData(), controller); // Act var result = controller.Create() as ViewResult; // Assert Assert.IsNotNull(result, TestContext.TestName); }
public void CreateStudentsTest() { //Arrange StudentController controller = new StudentController(); Mock <StudentViewModel> model = new Mock <StudentViewModel>(); //Act var result = controller.Create(model.Object) as RedirectToRouteResult; //Assert Assert.IsNotNull(result); Assert.AreEqual("Index", result.RouteValues["action"]); Assert.IsTrue(controller.ModelState.IsValid); }
public void Controller_Student_Create_Post_Data_Is_Null_Should_Return_Error_Page() { // Arrange StudentController controller = new StudentController(); var context = CreateMoqSetupForCookie(); controller.ControllerContext = new ControllerContext(context, new RouteData(), controller); // Act var result = (RedirectToRouteResult)controller.Create((StudentModel)null); // Assert Assert.AreEqual("Error", result.RouteValues["action"], TestContext.TestName); }
public void FailToCreateStudentsTest() { //Arrange StudentController controller = new StudentController(); controller.ModelState.AddModelError("Id", "Invalid id"); Mock <StudentViewModel> model = new Mock <StudentViewModel>(); //Act var result = controller.Create(model.Object) as ViewResult; //Assert Assert.IsNotNull(result); Assert.IsFalse(controller.ModelState.IsValid); }
public void StudentCreate() { StudentController sk = new StudentController(); Mock <UnitOfWork> mockUnit = new Mock <UnitOfWork>(new Zadatak2Context()); Mock <StudentRepository> mockStudent = new Mock <StudentRepository>(new Zadatak2Context()); mockStudent.Setup(x => x.Create(SeedClass.GetStudentsTest()[1])); mockUnit.Setup(x => x.SaveChanges()); sk.UnitOfWork = mockUnit.Object; sk.UnitOfWork.Studenti = mockStudent.Object; ViewResult rezultat = sk.Create(SeedClass.GetStudentsTest()[1]) as ViewResult; mockStudent.Verify((x => x.Create(It.IsAny <Student>())), Times.Once()); mockUnit.Verify((x => x.SaveChanges()), Times.Once()); }
public void Create_RedirectsToStudentsAndCreatesStudent_WhenRequestIsValid() { //Arrange Student student = new Student { Name = "New Student" }; StudentService studentService = Substitute.For <StudentService>(); StudentController controller = new StudentController(studentService); //Act var actionResult = controller.Create(student); studentService.Received().CreateStudent(student); // Assert actionResult.Should().BeOfType <RedirectToActionResult>().Which.ActionName.Should().Be("Students"); }
public void Create_ReturnsViewResult_WhenStudentModelStateIsInvalid() { //Arrange Student student = new Student { Name = "New 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.Create(student); //Assert Assert.IsAssignableFrom <ViewResult>(actual); actual.Should().BeAssignableTo <ViewResult>(); }
public void Controller_Student_Create_Post_Default_Should_Return_Index_Page() { // Arrange StudentController controller = new StudentController(); StudentModel data = DataSourceBackend.Instance.StudentBackend.GetDefault(); var context = CreateMoqSetupForCookie(); controller.ControllerContext = new ControllerContext(context, new RouteData(), controller); // Act var result = (RedirectToRouteResult)controller.Create(data); // Assert Assert.AreEqual("Index", result.RouteValues["action"], TestContext.TestName); }
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); } }
public void Create_Post_RedirectToStudents() { // Arrange Student studentForCreating = new Student(); var studentServiceMock = Substitute.For <StudentService>(); studentServiceMock.CreateStudent(studentForCreating).Returns(true); var controller = new StudentController(studentServiceMock); // Act var result = controller.Create(studentForCreating); // Assert var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Null(redirectToActionResult.ControllerName); Assert.Equal("Students", redirectToActionResult.ActionName); }
public void Controller_Student_Create_Post_Model_Is_Invalid_Should_Send_Back_For_Edit() { // Arrange StudentController controller = new StudentController(); StudentModel data = new StudentModel(); // Make ModelState Invalid controller.ModelState.AddModelError("test", "test"); var context = CreateMoqSetupForCookie(); controller.ControllerContext = new ControllerContext(context, new RouteData(), controller); // Act ViewResult result = controller.Create(data) as ViewResult; // Assert Assert.IsNotNull(result, TestContext.TestName); }
public async void Create_Errors() { StudentControllerMockFacade mock = new StudentControllerMockFacade(); var mockResponse = new Mock <CreateResponse <ApiStudentResponseModel> >(new FluentValidation.Results.ValidationResult()); var mockRecord = new ApiStudentResponseModel(); mockResponse.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiStudentRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiStudentResponseModel> >(mockResponse.Object)); StudentController controller = new StudentController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.Create(new ApiStudentRequestModel()); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiStudentRequestModel>())); }
public void CreateStudentShouldSaveTheStudent() { //ARRANGE var classId = Guid.NewGuid(); var classEntity = new ClassEntity(); var model = new CreateStudentViewModel { Class = new ClassEntity { Id = classId }, UserName = "******" }; var classLogic = new Mock <IClassLogic>(); classLogic .Setup(x => x.Get(classId)) .Returns(classEntity) .Verifiable("Should get the class to add the student to."); classLogic .Setup(x => x.Update(It.IsAny <ClassEntity>())) .Returns(classEntity) .Verifiable("Should add user to class"); var userLogic = new Mock <IUserLogic>(); userLogic .Setup(x => x.AddToRole(It.IsAny <string>(), "Student")) .Verifiable("Should create user"); var controller = new StudentController(userLogic.Object, null, null, classLogic.Object, null, null); //ACT var result = controller.Create(model) as RedirectToRouteResult; //ASSERT classLogic.Verify(); userLogic.Verify(); Assert.NotNull(result); Assert.AreEqual("Manage", result.RouteValues["Action"]); }
public async void Create_No_Errors() { StudentControllerMockFacade mock = new StudentControllerMockFacade(); var mockResponse = new CreateResponse <ApiStudentResponseModel>(new FluentValidation.Results.ValidationResult()); mockResponse.SetRecord(new ApiStudentResponseModel()); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiStudentRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiStudentResponseModel> >(mockResponse)); StudentController controller = new StudentController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.Create(new ApiStudentRequestModel()); response.Should().BeOfType <CreatedResult>(); (response as CreatedResult).StatusCode.Should().Be((int)HttpStatusCode.Created); var createResponse = (response as CreatedResult).Value as CreateResponse <ApiStudentResponseModel>; createResponse.Record.Should().NotBeNull(); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiStudentRequestModel>())); }
public void CreateStudentShouldDisplayTheCorrectView() { //ARRANGE var logic = new Mock <ISchoolLogic>(); logic .Setup(x => x.GetAll()) .Returns(new List <School>()) .Verifiable("Should get schools to select"); var controller = new StudentController(null, null, logic.Object, null, null, null); //ACT var result = controller.Create() as ViewResult; //ASSERT logic.Verify(); Assert.NotNull(result); Assert.NotNull(result.Model); Assert.That(result.ViewName, Is.EqualTo("Create")); }
public async void CanCreateStudent() { DbContextOptions <SchoolDbContext> options = new DbContextOptionsBuilder <SchoolDbContext>() .UseInMemoryDatabase("CreateStudentDB").Options; using (SchoolDbContext context = new SchoolDbContext(options)) { // arrange Student student1 = new Student(); student1.Name = "Bill Test"; student1.Level = Level.Undergraduate; student1.CourseID = 1; student1.EnrollmentTerm = EnrollmentTerm.Summer2018; // act StudentController testSC = new StudentController(context); await testSC.Create(student1); var results1 = context.Students.Where(s => s.Name == "Bill Test"); // assert there is one instance of the results Assert.Equal(1, results1.Count()); } }
public void Create() { ViewResult result = controller.Create() as ViewResult; Assert.AreEqual("Create", result.ViewName); }