Exemplo n.º 1
0
        public void SignupCourse_ThrowsException_When_Course_IsNull([Frozen] Mock <ICourseRepository> repository,
                                                                    CourseSignupCommand command,
                                                                    CoursesCommandService sut)
        {
            // Arrange
            repository.Setup(o => o.GetCourse(command.CourseId)).Returns <Course>(null);
            // Act
            Action act = () => sut.SignupCourse(command).GetAwaiter().GetResult();

            // Assert
            act.Should().Throw <Exception>($"because course is null").And.Message.Contains("Course not found");
        }
Exemplo n.º 2
0
        public async void SignupCourse_Success([Frozen] Mock <ICourseRepository> repository,
                                               CourseSignupCommand command,
                                               Course course,
                                               User user,
                                               CoursesCommandService sut)
        {
            // Arrange
            repository.Setup(o => o.GetCourse(command.CourseId)).ReturnsAsync(course);
            repository.Setup(o => o.GetUser(command.InputData.StudentId)).ReturnsAsync(user);
            int preSignUpUserCount = course.UserCourses.Count;
            // Act
            SignupCourseResponse result = await sut.SignupCourse(command);

            // Assert
            int postSignUpUserCount = course.UserCourses.Count;

            postSignUpUserCount.Should().Be(preSignUpUserCount + 1, "because user is added to the course");
            result.Success.Should().BeTrue("because result is success");
        }
        public async Task <SignupCourseResponse> SignupCourse(CourseSignupCommand command)
        {
            SignupCourseResponse result = new SignupCourseResponse();
            Course course = await CourseRepository.GetCourse(command.CourseId);

            if (course == null)
            {
                throw new Exception($"Course not found. CourseId: {command.CourseId}");
            }
            User user = await CourseRepository.GetUser(command.InputData.StudentId);

            if (user == null)
            {
                throw new Exception($"User not found. UserId: {command.InputData.StudentId}");
            }
            course.Signup(user);
            await UnitOfWork.Commit();

            result.Success = true;
            return(result);
        }