public async Task AddNewInstructor() { Mock <IInstructorRepository> mockInstructorRepository = new Mock <IInstructorRepository>(); Instructor addedInstructor = null; mockInstructorRepository.Setup(m => m.AddInstructor(It.IsAny <Instructor>())) .Returns(Task.CompletedTask) .Callback <Instructor>(m => addedInstructor = m); InstructorsController instructorsController = new InstructorsController (mockInstructorRepository.Object); Instructor instructor = new Instructor("Grahm", "Peace", "*****@*****.**", 1); instructor.InstructorID = 1; //act var result = await instructorsController.AddInstructor(instructor); Assert.Equal(instructor.InstructorLastName, addedInstructor.InstructorLastName); Assert.Equal(instructor, addedInstructor); }
public async Task GetAllInstructors_ReturnsInstructors() { var client = Factory.CreateClient(); var instructor1 = new Instructor { FirstName = "Stu", LastName = "Steiner" }; var instructor2 = new Instructor { FirstName = "Tom", LastName = "Capaul" }; var service = new Mock <IInstructorService>(); //service.Setup(x => x.GetAllInstructors()) // .Returns(Task.FromResult(new Collection<Instructor> { instructor1, instructor2 })) // .Verifiable(); var controller = new InstructorsController(service.Object, Mapper.Instance); var result = (await controller.GetAllInstructors()).Result as OkObjectResult; List <InstructorViewModel> instructors = ((IEnumerable <InstructorViewModel>)result.Value).ToList(); Assert.AreEqual(2, instructors.Count); AssertAreEqual(instructors[0], instructor1); AssertAreEqual(instructors[1], instructor2); service.VerifyAll(); }
public async Task CreateInstructor_ReturnsCreatedInstructor() { var instructor = new InstructorInputViewModel { FirstName = "Stu", LastName = "Steiner" }; var service = new Mock <IInstructorService>(); service.Setup(x => x.AddInstructor(It.Is <Instructor>(g => g.FirstName == instructor.FirstName))) .Returns(Task.FromResult(new Instructor { Id = 2, FirstName = instructor.FirstName, LastName = instructor.LastName })) .Verifiable(); var controller = new InstructorsController(service.Object, Mapper.Instance); var result = (await controller.AddInstructor(instructor)).Result as OkObjectResult; Instructor resultValue = (Instructor)result.Value; Assert.IsNotNull(resultValue); Assert.AreEqual(2, resultValue.Id); Assert.AreEqual("Stu", resultValue.FirstName); Assert.AreEqual("Steiner", resultValue.LastName); service.VerifyAll(); }
public async Task CreateInstructor_RequiresInstructor() { var service = new Mock <IInstructorService>(); var controller = new InstructorsController(service.Object, Mapper.Instance); var result = (await controller.AddInstructor(null)).Result as BadRequestResult; Assert.IsNotNull(result); }
public async Task DeleteInstructor_RequiresPositiveId(int instructorId) { var service = new Mock <IInstructorService>(MockBehavior.Strict); var controller = new InstructorsController(service.Object, Mapper.Instance); var result = await controller.DeleteInstructor(instructorId); Assert.IsTrue(result is BadRequestObjectResult); }
public async Task IndexInstructorTest() { InstructorsController obj = new InstructorsController(_context); var actResult = await obj.Index(13, null) as ViewResult; var model = actResult.Model as InstructorIndexData; var courses = model.Courses; Assert.That(courses.Cast <object>().Count(), Is.GreaterThan(0)); Assert.That(courses.Cast <object>().Count(), Is.EqualTo(2)); Assert.That(actResult.ViewName, Is.EqualTo(null)); }
public async Task DeleteInstructor_ReturnsOkWhenInstructorIsDeleted() { var service = new Mock <IInstructorService>(); service.Setup(x => x.DeleteInstructor(2)) .Returns(Task.FromResult(true)) .Verifiable(); var controller = new InstructorsController(service.Object, Mapper.Instance); var result = await controller.DeleteInstructor(2); Assert.IsTrue(result is OkResult); service.VerifyAll(); }
public async Task DeleteInstructor_ReturnsNotFoundWhenTheInstructorFailsToDelete() { var service = new Mock <IInstructorService>(); service.Setup(x => x.DeleteInstructor(2)) .Returns(Task.FromResult(false)) .Verifiable(); var controller = new InstructorsController(service.Object, Mapper.Instance); var result = await controller.DeleteInstructor(2); Assert.IsTrue(result is NotFoundResult); service.VerifyAll(); }
public void SetLinks_Instructors() { var controller = new InstructorsController(); controller.Request = new HttpRequestMessage(HttpMethod.Get, APIURL); controller.Configuration = new HttpConfiguration(); controller.Configuration.Routes.MapHttpRoute( name: ROUTENAME, routeTemplate: ROUTETEMPLATE, defaults: new { id = RouteParameter.Optional }); List <LinkModel> links = EnrollMe.Controllers.Helper.SetLinks(controller.Request.GetUrlHelper(), InstructorsController.ROUTENAME, "Instructors", "Get"); Assert.IsTrue(links.Count > 0); Assert.IsTrue(links[0].Href.Contains(APIURL)); }
public InstructorsControllerTests(ITestOutputHelper output) { this.output = output; mockDepartmentRepo = Departments().AsMockRepository(); mockInstructorRepo = Instructors().AsMockPersonRepository(); mockCourseRepo = Courses().AsMockRepository(); mockCourseAssignmentRepo = CourseAssignments().AsMockRepository(); mockModelBindingHelperAdaptor = new Mock <IModelBindingHelperAdaptor>(); var mockUnitOfWork = new Mock <UnitOfWork <ApplicationContext> >(); mockUnitOfWork.Setup(c => c.DepartmentRepository).Returns(mockDepartmentRepo.Object); mockUnitOfWork.Setup(c => c.InstructorRepository).Returns(mockInstructorRepo.Object); mockUnitOfWork.Setup(c => c.CourseRepository).Returns(mockCourseRepo.Object); mockUnitOfWork.Setup(c => c.CourseAssignmentRepository).Returns(mockCourseAssignmentRepo.Object); sut = new InstructorsController(mockUnitOfWork.Object, mockModelBindingHelperAdaptor.Object); }
public void ReturnViewForListAllInstructors() { Mock <IInstructorRepository> mockInstructorRepository = new Mock <IInstructorRepository>(); List <Instructor> mockInstructorList = PopulateInstructors(); mockInstructorRepository .Setup(m => m.ListAllInstructors()) .Returns(mockInstructorList); InstructorsController instructorsController = new InstructorsController (mockInstructorRepository.Object); var result = instructorsController.ListAllInstructors(); ViewResult viewResult = Assert.IsType <ViewResult>(result); List <Instructor> viewResultModel = (List <Instructor>)viewResult.Model; int expectedNumberOfInstructors = 5; Assert.Equal(expectedNumberOfInstructors, viewResultModel.Count); string expectedEmail = "*****@*****.**"; string actualEmail = viewResultModel .Find(i => i.InstructorID == 1) .InstructorEmail; Assert.Equal(expectedEmail, actualEmail); Assert.Equal(mockInstructorList, viewResultModel); }
static void TestInstructorsController() { var conn = new Connection(Server, Instance, Database); var InstCtrl = new InstructorsController(conn); var albert = new Instructor() { Firstname = "Albert", Lastname = "Einstein", YearsExperience = 50, IsTenured = false }; var result = InstCtrl.Insert(albert); albert = InstCtrl.GetByLastname("Einstein"); albert.IsTenured = true; result = InstCtrl.Update(albert); result = InstCtrl.Delete(albert.Id); var instructor1 = InstCtrl.GetByPK(1); var instructors = InstCtrl.GetAll(); conn.Close(); }
public async Task UpdateInstructor_ReturnsUpdatedInstructor() { var instructor = new InstructorInputViewModel { FirstName = "Stu", LastName = "Steiner" }; var service = new Mock <IInstructorService>(); //service.Setup(x => x.GetById(2)) // .Returns(Task.FromResult(new Instructor // { // Id = 2, // Name = instructor.Name // })) // .Verifiable(); var controller = new InstructorsController(service.Object, Mapper.Instance); var result = (await controller.UpdateInstructor(2, instructor)); Assert.IsNotNull(result); service.VerifyAll(); }
public async Task IndexTestAsync() { InstructorsController obj = new InstructorsController(_context); var actResult = await obj.Index(null, null) as ViewResult; var model = actResult.Model as InstructorIndexData; var instructors = model.Instructors as IEnumerable; Assert.That(instructors.Cast <object>().Count(), Is.EqualTo(5)); Assert.That(actResult.ViewName, Is.EqualTo(null)); IEnumerator enumerator = instructors.GetEnumerator(); enumerator.MoveNext(); Instructor instructor = (Instructor)enumerator.Current; CourseAssignment ca = instructor.CourseAssignments.First(); Course course = ca.Course; Department department = course.Department; Assert.That(course, Is.Not.Null); Assert.That(department, Is.Not.Null); }
static void Main(string[] args) { // GET ALL EXERCISES ExercisesController exercisesController = new ExercisesController(); var exercises = exercisesController.GetAllExercises(); exercises.ForEach(exercise => { Console.WriteLine($"{exercise.Id}: {exercise.ExerciseName} -- {exercise.ExerciseLanguage}"); Console.WriteLine(" "); }); Pause(); // Find all the exercises in the database where the language is JavaScript. var selectExercises = exercisesController.GetExerciseByLanguage("C#"); selectExercises.ForEach(exercise => { Console.WriteLine($"{exercise.Id}: {exercise.ExerciseName} -- {exercise.ExerciseLanguage}"); Console.WriteLine(" "); }); Pause(); // Insert a new exercise into the database. Exercise exerciseToAdd = new Exercise { ExerciseName = "Personal Website", ExerciseLanguage = "ReactJS" }; //exercisesController.AddExercise(exerciseToAdd); Pause(); // Insert a new instructor into the database. Assign the instructor to an existing cohort. InstructorsController instructorsController = new InstructorsController(); Instructor instructorToAdd = new Instructor { FirstName = "Todd", LastName = "Packer", SlackHandle = "@tpDaddy", CohortId = 2, Specialty = "Hitting on Women" }; instructorsController.AddInstructor(instructorToAdd); Pause(); // Assign an existing exercise to an existing student. AssignmentsController assignmentsController = new AssignmentsController(); Assignment assignmentToAssign = new Assignment { StudentId = 1, InstructorId = 2, ExerciseId = 3 }; assignmentsController.AssignExercise(assignmentToAssign); Pause(); // Find all the students in the database. Include each student's cohort AND each student's list of exercises. StudentsController studentsController = new StudentsController(); var students = studentsController.GetAllStudents(); // HOW TO ONLY SHOW STUDENTS ONCE??? students.ForEach(student => { Console.WriteLine($"{student.Id}: {student.FirstName}{student.LastName} [{student.cohort.Title}] -- "); Console.WriteLine("Exercises:"); exercises.ForEach(ex => { Console.WriteLine($"{ex.ExerciseName} -- {ex.ExerciseLanguage}"); }); }); Pause(); }