//[Fact]
        public void AssignStudents_Post_ReturnsNotFound_WhenNonExistingCourseInModel()
        {
            // Arrange
            int    nonExistingCourseId = 0;
            Course nonExistingCourse   = new Course()
            {
                Id = nonExistingCourseId
            };
            CourseStudentsAssignmentViewModel model = new CourseStudentsAssignmentViewModel()
            {
                Course = nonExistingCourse,
                StudentsAssignmentsList = new List <StudentAssignment>()
            };

            var courseServiceMock = Substitute.For <CourseService>();

            courseServiceMock.IsCourseExisted(nonExistingCourse).Returns(false);
            var controller = new CourseController(courseServiceMock, null, null, null, null);

            // Act
            var result = controller.AssignStudents(model);

            // Assert
            var viewResult = Assert.IsType <NotFoundResult>(result);
        }
        public void AssignStudents_Post_RedirectToCourses()
        {
            // Arrange
            int       courseId             = 1;
            const int assignedStudentId    = 2;
            const int nonAssignedStudentId = 3;
            Course    course = new Course()
            {
                Id = courseId
            };
            CourseStudentsAssignmentViewModel model = new CourseStudentsAssignmentViewModel()
            {
                Course = course,
                StudentsAssignmentsList = new List <StudentAssignment>()
                {
                    new StudentAssignment()
                    {
                        Student = new Student()
                        {
                            Id = assignedStudentId
                        },
                        IsAssigned = true
                    },
                    new StudentAssignment()
                    {
                        Student = new Student()
                        {
                            Id = nonAssignedStudentId
                        },
                        IsAssigned = false
                    }
                }
            };

            var courseServiceMock = Substitute.For <CourseService>();

            courseServiceMock.IsCourseExisted(course).Returns(true);
            courseServiceMock.AssignStudentsToCourse(courseId, new List <int>()).Returns(true);

            var studentServiceMock = Substitute.For <StudentService>();
            //studentServiceMock.IsStudentExisted(new Student()).Returns(true);

            var controller = new CourseController(courseServiceMock, studentServiceMock, null, null, null);

            // Act
            var result = controller.AssignStudents(model);

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

            Assert.Null(redirectToActionResult.ControllerName);
            Assert.Equal("Courses", redirectToActionResult.ActionName);
        }
Ejemplo n.º 3
0
        public async Task AssignStudents_ReturnsBadRequest_WhenAssignmentViewModelIsEmpty()
        {
            //Arrange
            CourseStudentAssignmentViewModel expectedModel = null;
            CourseService  courseService  = Substitute.For <CourseService>();
            StudentService studentService = Substitute.For <StudentService>();
            var            controller     = new CourseController(courseService, studentService, null);

            //Act
            IActionResult actual = controller.AssignStudents(expectedModel);

            //Assert
            actual.Should().BeAssignableTo <BadRequestResult>();
        }
Ejemplo n.º 4
0
        public async Task AssignStudents_ReturnNotFound_WhenNonExistingCourseId()
        {
            //Arrange
            CourseService    courseService = Substitute.For <CourseService>();
            CourseController controller    = new CourseController(courseService, null, null);

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

            //Assert
            var expected = new NotFoundResult();

            actual.Should().BeEquivalentTo(expected);
        }
        public void AssignStudents_Get_ReturnsNotFound_WhenNonExistingCourseId()
        {
            // Arrange
            int nonExistingCourseId = 0;

            var courseServiceMock = Substitute.For <CourseService>();

            courseServiceMock.GetCourse(nonExistingCourseId).Returns(default(Course));
            var controller = new CourseController(courseServiceMock, null, null, null, null);

            // Act
            var result = controller.AssignStudents(nonExistingCourseId);

            // Assert
            var viewResult = Assert.IsType <NotFoundResult>(result);
        }
Ejemplo n.º 6
0
        public void AssignStudents_ReturnsViewResult_WithViewModel()
        {
            // Arrange
            var courseServiceMock = Substitute.For <CourseService>();

            courseServiceMock.GetCourseById(Arg.Any <int>()).Returns(new Course());
            var studentService = Substitute.For <StudentService>();

            studentService.GetAllStudents().Returns(new List <Student>());
            var controller = new CourseController(courseServiceMock, studentService);

            // Act
            var result = controller.AssignStudents(0);
            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.IsAssignableFrom <CourseStudentAssignmentViewModel>(viewResult.ViewData.Model);
        }
        public void AssignStudents_Get_ReturnsViewResult_WithCourseStudentsAssignmentViewModel()
        {
            // Arrange
            const int    courseId             = 1;
            const string courseName           = "test";
            const int    assignedStudentId    = 2;
            const int    nonAssignedStudentId = 3;

            var allStudents = new List <Student>()
            {
                new Student()
                {
                    Id = assignedStudentId
                },
                new Student()
                {
                    Id = nonAssignedStudentId
                }
            };
            CourseStudentsAssignmentViewModel expectedModel = new CourseStudentsAssignmentViewModel()
            {
                Course = new Course()
                {
                    Id       = courseId,
                    Name     = courseName,
                    Students = new List <Student>()
                    {
                        new Student()
                        {
                            Id = assignedStudentId
                        }
                    }
                },
                StudentsAssignmentsList = new List <StudentAssignment>()
                {
                    new StudentAssignment()
                    {
                        Student = new Student()
                        {
                            Id = assignedStudentId
                        },
                        IsAssigned = true
                    },
                    new StudentAssignment()
                    {
                        Student = new Student()
                        {
                            Id = nonAssignedStudentId
                        },
                        IsAssigned = false
                    }
                }
            };

            var courseServiceMock = Substitute.For <CourseService>();

            courseServiceMock.GetCourse(courseId).Returns(new Course()
            {
                Id       = courseId,
                Name     = courseName,
                Students = new List <Student>()
                {
                    new Student()
                    {
                        Id = assignedStudentId
                    }
                }
            });

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

            studentService.GetAllStudents().Returns(allStudents);

            var controller = new CourseController(courseServiceMock, studentService, null, null, null);

            // Act
            var result = controller.AssignStudents(courseId);

            // Assert
            var viewResult  = Assert.IsType <ViewResult>(result);
            var actualModel = Assert.IsType <CourseStudentsAssignmentViewModel>(viewResult.Model);

            actualModel.WithDeepEqual(expectedModel).Assert();
        }
Ejemplo n.º 8
0
        public void AssignStudents_ReturnsViewResult_WithCoursesAndStudentsAreAssigned()
        {
            // Arrange
            const int    courseId             = 1;
            const int    assignedStudentId    = 11;
            const int    nonAssignedStudentId = 22;
            const string courseName           = "test";

            CourseStudentAssignmentViewModel expectedModel = new CourseStudentAssignmentViewModel()
            {
                Id       = courseId,
                Name     = courseName,
                Students = new List <AssignmentStudentViewModel>()
                {
                    new AssignmentStudentViewModel()
                    {
                        StudentId = assignedStudentId, StudentFullName = "Test1", IsAssigned = true
                    },
                    new AssignmentStudentViewModel()
                    {
                        StudentId = nonAssignedStudentId, StudentFullName = "Test2", IsAssigned = false
                    }
                }
            };
            var courseServiceMock = Substitute.For <CourseService>();

            courseServiceMock.GetCourseById(courseId).Returns(new Course()
            {
                Id       = courseId,
                Name     = courseName,
                Students = new List <Student>()
                {
                    new Student()
                    {
                        Id = assignedStudentId
                    }
                }
            });

            var studentService = Substitute.For <StudentService>();
            var students       = new List <Student>()
            {
                new Student()
                {
                    Id = assignedStudentId, Name = "Test1"
                },
                new Student()
                {
                    Id = nonAssignedStudentId, Name = "Test2"
                }
            };

            studentService.GetAllStudents().Returns(students);
            var controller = new CourseController(courseServiceMock, studentService);

            // Act
            var result = controller.AssignStudents(courseId);

            // Assert
            var viewResult  = Assert.IsType <ViewResult>(result);
            var actualModel = Assert.IsAssignableFrom <CourseStudentAssignmentViewModel>(viewResult.ViewData.Model);

            actualModel.Should().BeEquivalentTo(expectedModel);
        }
Ejemplo n.º 9
0
        //  [Fact]
        public async Task AssignStudents_SetStudentsToCoursesAndRedirectsToCourses_WhenCoursesAndStudentsAreAssigned()
        {
            //Arrange
            const int    courseId             = 1;
            const int    assignedStudentId    = 11;
            const int    nonAssignedStudentId = 22;
            const string courseName           = "test";

            CourseStudentAssignmentViewModel expectedModel = new CourseStudentAssignmentViewModel()
            {
                Id       = courseId,
                Name     = courseName,
                Students = new List <StudentViewModel>()
                {
                    new StudentViewModel()
                    {
                        StudentId = assignedStudentId, StudentFullName = "Test1", IsAssigned = true
                    },
                    new StudentViewModel()
                    {
                        StudentId = nonAssignedStudentId, StudentFullName = "Test2", IsAssigned = false
                    }
                }
            };
            var courseServiceMock = Substitute.For <CourseService>();

            courseServiceMock.GetCourse(courseId).Returns(new Course()
            {
                Id       = courseId,
                Name     = courseName,
                Students = new List <StudentCourse>()
                {
                    new StudentCourse()
                    {
                        StudentId = assignedStudentId
                    }
                }
            });

            var studentService = Substitute.For <StudentService>();
            var students       = new List <Student>()
            {
                new Student()
                {
                    Id = assignedStudentId, Name = "Test1"
                },
                new Student()
                {
                    Id = nonAssignedStudentId, Name = "Test2"
                }
            };

            studentService.GetAllStudents().Returns(students);
            var controller = new CourseController(courseServiceMock, studentService, null);

            // Act
            var actionResult = controller.AssignStudents(courseId);

            // Assert
            actionResult.Should().BeOfType <RedirectToActionResult>().Which.ActionName.Should().Be("Courses");
        }