public bool CreateCourse([FromBody] CreateCourseCommand request) { CourseContext context = HttpContext.RequestServices.GetService(typeof(CourseContext)) as CourseContext; CreateCourseHandler handler = new CreateCourseHandler(context); return(handler.Handle(request)); }
public void CreateCourseTest() { // arrange List <MedicalCourse> list = new List <MedicalCourse>(); MedicalCourse expected = new MedicalCourse { Name = "Ёндокринологи¤", Qualification = 1 }; CreateCourseCommand command = new CreateCourseCommand { Name = "Ёндокринологи¤", Qualification = 1 }; //act Course.Data.CourseContext context = new Course.Data.CourseContext(connString); CreateCourseHandler handler = new CreateCourseHandler(context); handler.Handle(command); using (conn = new MySqlConnection(connString)) { conn.Open(); string query = string.Format("select * from Courses where course_name='Ёндокринологи¤' and qualification=1"); MySqlCommand cmd = new MySqlCommand(query, conn); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { list.Add(new MedicalCourse() { Id = Convert.ToInt32(reader["course_id"]), Name = reader["course_name"].ToString(), Qualification = Convert.ToInt32(reader["qualification"]) }); } } } if (list[0].Name == expected.Name && list[0].Qualification == expected.Qualification) { Assert.IsTrue(true); } }
public void CreateCourseTest_Should_Throw_Validation_Exception() { //Arrange var request = new CreateCourseCommand { Name = "Math", Price = 5000, StartTime = new DateTime(2021, 01, 30, 10, 00, 00), EndTime = new DateTime(2021, 02, 04, 12, 00, 00), }; var handler = new CreateCourseHandler(_mapper, _fakeRepository); //Act && Assert Assert.ThrowsAsync <CourseValidationException>(() => handler.Handle(request, new CancellationToken())); }
public async Task CreateCourseTest_Should_Return_Id() { //Arrange var request = new CreateCourseCommand { Name = "Math", Price = 5000, StartTime = new DateTime(2021, 02, 02, 10, 00, 00), EndTime = new DateTime(2021, 02, 04, 12, 00, 00), }; var handler = new CreateCourseHandler(_mapper, _fakeRepository); //Act var course = await handler.Handle(request, new CancellationToken()); //Assert Assert.AreEqual(course, 3); }
public async Task Handle_should_create_entity() { var repo = NSubstitute.Substitute.For <ICoursesRepository>(); var unitOfWork = NSubstitute.Substitute.For <ISchoolUnitOfWork>(); unitOfWork.CoursesRepository.ReturnsForAnyArgs(repo); var validator = NSubstitute.Substitute.For <IValidator <CreateCourse> >(); validator.ValidateAsync(null, CancellationToken.None) .ReturnsForAnyArgs(ValidationResult.Successful); var sut = new CreateCourseHandler(validator, unitOfWork); var command = new CreateCourse(Guid.NewGuid(), "new course"); await sut.Handle(command, CancellationToken.None); repo.Received(1).CreateAsync(Arg.Is((Course c) => c.Id == command.CourseId && c.Title == command.CourseTitle), Arg.Any <CancellationToken>()); unitOfWork.Received(1).CommitAsync(Arg.Any <CancellationToken>()); }
public async Task Handle_should_create_entity() { await using var dbContext = _fixture.BuildDbContext(); var studentsRepo = new StudentsRepository(dbContext); var coursesRepo = new CoursesRepository(dbContext); var messagesRepository = NSubstitute.Substitute.For <IMessagesRepository>(); var eventSerializer = NSubstitute.Substitute.For <IEventSerializer>(); var unitOfWork = new SchoolUnitOfWork(dbContext, coursesRepo, studentsRepo, messagesRepository, eventSerializer); var sut = new CreateCourseHandler(new FakeValidator <CreateCourse>(), unitOfWork); var command = new CreateCourse(Guid.NewGuid(), "new course"); await sut.Handle(command, CancellationToken.None); var createdCourse = await coursesRepo.FindByIdAsync(command.CourseId, CancellationToken.None); createdCourse.Should().NotBeNull(); createdCourse.Id.Should().Be(command.CourseId); createdCourse.Title.Should().Be(command.CourseTitle); }