private async Task CreateStudentOrTeacher(AppUser appUser, User user) { if (appUser.IsStudent) { var currentStudent = await _userManager.FindByNameAsync(appUser.UserName); await _userManager.AddToRoleAsync(currentStudent, "student"); await _studentsController.Create(new Student { FirstName = user.FirstName, LastName = user.LastName, PhoneNumber = "000-000-000", Email = user.Email }); } if (appUser.IsTeacher) { var currentTeacher = await _userManager.FindByNameAsync(appUser.UserName); await _userManager.AddToRoleAsync(currentTeacher, "teacher"); await _teachersController.Create(new Teacher { FirstName = user.FirstName, LastName = user.LastName, PhoneNumber = "000-000-000", Email = user.Email }); } }
public void CheckThatTheIndexReturnsBasedOnSearch() { DbContextOptions <Lab13StudentEnrollmentDbContext> options = new DbContextOptionsBuilder <Lab13StudentEnrollmentDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; using (Lab13StudentEnrollmentDbContext context = new Lab13StudentEnrollmentDbContext(options)) { Student student = new Student(); student.Name = "Bob"; student.Age = 24; student.CourseID = "cs201"; Student student2 = new Student(); student.Name = "Joe"; student.Age = 22; student.CourseID = "cs301"; StudentsController sc = new StudentsController(context); var x = sc.Create(student); x = sc.Create(student2); var results = sc.Index("").IsCompletedSuccessfully; Assert.True(results); } }
public void CreateStudentViewLoads() { //act var actual = (ViewResult)controller.Create(); //assert Assert.AreEqual("Create", actual.ViewName); }
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 void CreateMethod_AddNewStudent_Correct() { //Arrange var dbLayer = new Mock <IStudentsRepository>(); var newStudent = new Student { FirstName = "Jan", LastName = "Kowalski", Address = "Kolorowa 12" }; dbLayer.Setup(d => d.AddStudent(newStudent)).Returns(20); var cont = new StudentsController(dbLayer.Object); //Act var result = cont.Create(newStudent); //Assert Assert.IsNotNull(result); Assert.IsTrue(result is RedirectToActionResult); var vr = (RedirectToActionResult)result; Assert.IsTrue(vr.ActionName == "Index"); Assert.IsTrue(vr.RouteValues["newStudentId"].ToString() == "20"); }
public async Task CreateStudentThenVerifiedCreated() { //Arrange _controller = Container.GetInstance <StudentsController>("StudentControllerFake"); var student = new Student() { Name = "Testing Creation Name", LastName = "Testing Creation LastName", Age = 20, Email = "*****@*****.**", Phone = "22222222", DateBirth = DateTime.UtcNow }; //Act var result = await _controller.Create(student); //Assert _dbContext = Container.GetInstance <ApplicationDbContext>("DatabaseFake"); var students = _dbContext.Students.ToList(); students.Should().HaveCountLessOrEqualTo(3); students.Last().Name.Should().Be(student.Name); students.Last().LastName.Should().Be(student.LastName); Container.Dispose(); }
public void TestCreateAction() { var controller = new StudentsController(); var result = controller.Create() as ViewResult; Assert.IsNotNull(result); }
public void StudentCanBeDelete() { DbContextOptions <Lab13StudentEnrollmentDbContext> options = new DbContextOptionsBuilder <Lab13StudentEnrollmentDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; using (Lab13StudentEnrollmentDbContext context = new Lab13StudentEnrollmentDbContext(options)) { Student student = new Student(); student.Name = "Bob"; student.Age = 24; student.CourseID = "cs201"; StudentsController sc = new StudentsController(context); var x = sc.Create(student); var retrieve = context.Students.Where(s => s.Name == "Bob").ToList(); ViewResult current = (ViewResult)sc.Details(retrieve[0].ID).Result; StudentDetailsViewModel test = (StudentDetailsViewModel)current.Model; Assert.Equal("Bob", test.Student.Name); x = sc.Delete(test.Student.ID); retrieve = context.Students.Where(s => s.Name == "Bob").ToList(); Assert.Empty(retrieve); } }
public static void Main(string[] args) { var ctrl = new StudentsController(); var student = ctrl.GetById(1); //Console.WriteLine(student.FullName); //var payments = new List<PaymentPostModel>(); ////payments.Add(new PaymentPostModel { Date = DateTime.Now, Value = 15500 }); ////payments.Add(new PaymentPostModel { Date = DateTime.Now, Value = 2000 }); ////payments.Add(new PaymentPostModel { Date = DateTime.Now, Value = 2200 }); ////payments.Add(new PaymentPostModel { Date = DateTime.Now, Value = 23330 }); //var addFirstStudent = new StudentPostModel() //{ // FirstName = "petya", // LastName = "1111", // Age = 25, // Payments = payments //}; ctrl.Create(addFirstStudent); var test = ctrl.StudentPayLes(100000); Console.ReadKey(); }
public void Students_Controller_Test_On_CreateModel_With_Invalid_Model() { //Arrange Guid id = new Guid("f616cc8c-2223-4145-b7d0-232a1f6f0795"); string firstName = "a"; string lastName = "TestL"; string userName = "******"; string password = "******"; string emailAdress = "TestE"; Student expectedStudent = new Student(firstName, lastName, userName, password, emailAdress); expectedStudent.Id = id; StudentModel expectedModel = new StudentModel(); expectedModel.EmailAddress = " "; var repo = Substitute.For <IRepository>(); var sut = new StudentsController(repo); repo.Create(expectedStudent); //Act sut.ModelState.AddModelError("FirstName", "Firstname Required"); var actual = sut.Create(expectedModel).Result; //Assert Assert.IsInstanceOfType(actual, typeof(ViewResult)); }
public void TestStudentController() { var controller = new StudentsController(fixture); // Act var result = controller.Create() as ViewResult; //Assert Assert.Equal("Create", result.ViewName); }
public void TestCreateStudentInvalidPhone() { var controller = new StudentsController(); Student student = this.CreateMockedStudent(); student.PhoneNumber = "556677889900"; var result = controller.Create(student) as ViewResult; Assert.IsFalse(controller.ModelState.IsValid); }
public void TestCreateStudentInvalidBirthday() { var controller = new StudentsController(); Student student = this.CreateMockedStudent(); student.Birthday = DateTime.Now; var result = controller.Create(student) as ViewResult; Assert.IsFalse(controller.ModelState.IsValid); }
public void TestCreateStudentEmptyLastName() { var controller = new StudentsController(); Student student = this.CreateMockedStudent(); student.LastName = ""; var result = controller.Create(student) as ViewResult; Assert.IsFalse(controller.ModelState.IsValid); }
public async Task Create_ReturnsRedirectToActionIn() { //Arrange var controller = new StudentsController(_studentRepository); // Act var result = await controller.Create(new Student { Id = 4, Email = "*****@*****.**", FristName = "Damien", LastName = "Alain" }) as RedirectToActionResult; //assert Assert.IsNotNull(result); Assert.AreEqual("Index", result.ActionName); }
public void Students_Controller_Test_On_Create() { //Arrange var repo = Substitute.For <IRepository>(); var sut = new StudentsController(repo); //Act var actual = sut.Create(); //Assert Assert.IsInstanceOfType(actual, typeof(ViewResult)); }
public async Task Create_ShouldReturnJsonAnonymousObject_WhenInvalidObjectPassed() { // Arrange var course = new Student() { LastName = "Advanced", Email = "*****@*****.**" }; _controller.ModelState.AddModelError("FirstName", "Required"); // Act dynamic response = await _controller.Create(course); var valueFromAnnonymous = response.Value.GetType().GetProperty("text").GetValue(response.Value, null); // Assert Assert.IsType <JsonResult>(response); Assert.NotNull(response); Assert.Equal("Invalid Student model!", valueFromAnnonymous); }
public async Task Creating_Student() { var repo = new StudentRepository(_context); var logic = new StudentLogic(repo); var controller = new StudentsController(_context, logic); var stud = new Student { FirstMidName = "Carson test", LastName = "Alexander test", EnrollmentDate = DateTime.Parse("2005-09-01"), DepartmentId = 1 }; var result = await controller.Create(stud); //https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc //result.Should().BeOfType<RedirectToActionResult>().Which.ActionName.Should().Be("Index"); }
public void Create_Student() { var repo = new StudentRepository(_context); var logic = new StudentLogic(repo); var controller = new StudentsController(_context, logic); // Act var result = controller.Create(); // Assert var viewResult = Assert.IsType<ViewResult>(result); //var model = Assert.IsAssignableFrom<IEnumerable<StormSessionViewModel>>(viewResult.ViewData.Model); //Assert.Equal(2, model.Count()); }
public static void StudentsTest() { var repository = new StudentDapperRepository(); var licenseRepository = new LicenseEFRepository(); var service = new StudentService(repository, licenseRepository); var controller = new StudentsController(service); var studentPostModel = new StudentPostModel { FirstName = "Puzo", LastName = "Moe", LicenseNum = 100 }; controller.Create(studentPostModel); var students = controller.GetAll(); }
public static void Menu() { Console.WriteLine("\nSelect following options \n"); bool terminate = true; do { Console.WriteLine("1.Add student \n2.Get a list of students \n3.Update a student \n4.Delete a student \n5.Exit program"); bool loopState = true; while (loopState) { switch (int.TryParse(Console.ReadLine(), out int value) ? value : 0) { case (int)HomeMenu.Create: StudentsController.Create(); loopState = false; break; case (int)HomeMenu.Read: Console.Clear(); Console.WriteLine("List of students\n"); StudentsController.Read(); loopState = false; break; case (int)HomeMenu.Update: StudentsController.Update(int.Parse(Utility.Ask("Select student by ID", Student.DefaultValueId))); loopState = false; break; case (int)HomeMenu.Delete: StudentsController.Delete(int.Parse(Utility.Ask("Select a student to delete by Student ID", Student.DefaultValueId))); loopState = false; break; case (int)HomeMenu.Exit: loopState = false; terminate = false; break; default: Console.WriteLine("Please choose between 1-5 again"); break; } } } while (terminate); }
public async Task Create_InvalidModelState() { //Arrange var studentsRepositoryMock = new Mock <IStudentsRepository>(); var controller = new StudentsController(studentsRepositoryMock.Object); // Act controller.ModelState.AddModelError("Email", "Required"); var viewResult = await controller.Create(new Student()) as ViewResult; //assert Assert.IsNotNull(viewResult); var student = viewResult.Model as Student; Assert.IsNotNull(student); }
private void Student_Controller_Add_Test() { StudentsController studentController = getController <StudentsController>(); ActionResult <StudentPoco> result = studentController.Create(_student); Assert.IsNotInstanceOf(typeof(OkResult), result); OkObjectResult oResult = (OkObjectResult)result.Result; Assert.IsNotNull(oResult); StudentPoco poco = (StudentPoco)(oResult).Value; Assert.IsNotNull(poco); Assert.IsTrue(_student.Name.Equals(poco.Name)); Assert.IsTrue(_student.Age == poco.Age); _student = poco; }
public void TestMethod1() { Mock <IStudentsRepository> mockRepo = new Mock <IStudentsRepository>(); StudentsController studentsController = new StudentsController(mockRepo.Object); Student s = new Student { Firstname = "Daniel", Lastname = "Something" }; studentsController.Create(s, null); mockRepo.Verify(a => a.InsertOrUpdate(s, null)); mockRepo.Verify(a => a.Save()); }
public async Task TestControllerAsync() { //http://stackoverflow.com/questions/39481353/how-do-i-moq-the-applicationdbcontext-in-net-core // Need to setup the ApplicationDbContext, since this is done in Startup.cs which is not part of test project. var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>(); optionsBuilder.UseInMemoryDatabase(); //had to add package Microsoft.EntityFrameworkCore.InMemory var _dbContext = new ApplicationDbContext(optionsBuilder.Options); var controller = new StudentsController(_dbContext); // Act var result = controller.Create() as ViewResult; //output.WriteLine("view name: " + result.ViewName ); //debug output. //Assert Assert.Equal("Create", result.ViewName); }
public void TestMethodEmail() { Mock <IStudentsRepository> mockRepo = new Mock <IStudentsRepository>(); Mock <IEmailer> fakeEmailer = new Mock <IEmailer>(); StudentsController controller = new StudentsController(mockRepo.Object, fakeEmailer.Object); Student s = new Student { Firstname = "Daniel", Lastname = "Something" }; controller.Create(s, null); fakeEmailer.Verify(a => a.Send("Welcome to our website...")); }
public async Task SampleControllerTest() { //Arrange SchoolContext schoolContext = GetSchoolContext(); StudentsController studentController = new StudentsController(schoolContext); //Act Student newStudent = new Student() { FirstName = "Keith", LastName = "Joel", EnrollmentDate = DateTime.Now }; await studentController.Create(newStudent); Student dbStudent = schoolContext.Students .Where(student => student.FirstName == newStudent.FirstName && student.LastName == newStudent.LastName).SingleOrDefault(); Assert.NotNull(dbStudent); }
public void TestMethod1() { //Arrange Mock <IStudentsRepository> mockRepo = new Mock <IStudentsRepository>(); StudentsController controller = new StudentsController(mockRepo.Object, null); Student s = new Student { Firstname = "Daniel", Lastname = "Something" }; //Act - Where you call the method to test controller.Create(s, null); //Assert - verify that insertorupdate was called mockRepo.Verify(a => a.InsertOrUpdate(s)); mockRepo.Verify(a => a.Save()); }
public void DatabaseCanSave() { DbContextOptions <Lab13StudentEnrollmentDbContext> options = new DbContextOptionsBuilder <Lab13StudentEnrollmentDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; using (Lab13StudentEnrollmentDbContext context = new Lab13StudentEnrollmentDbContext(options)) { //Arrange Student student = new Student(); student.Name = "Bob"; student.Age = 24; student.CourseID = "cs201"; //Act StudentsController sc = new StudentsController(context); var x = sc.Create(student); var results = context.Students.Where(s => s.Name == "Bob"); //Assert Assert.Equal(1, results.Count()); } }
public void Create_Saves_A_Student_Via_Context() { var student = new Student { EnrollmentDate = new DateTime(1986, 01, 23), FirstMidName = "Brent", LastName = "Sullivan" }; var mockSet = new Mock <DbSet <Student> >(); var mockContext = new Mock <SchoolContext>(); mockContext.Setup(m => m.Students).Returns(mockSet.Object); var controller = new StudentsController(mockContext.Object); controller.Create(student); mockSet.Verify(m => m.Add(student), Times.Once); mockContext.Verify(m => m.SaveChanges(), Times.Once); }