public CreateStudentViewModel()
 {
     SelectedStudent      = new Student();
     StudentCatalog       = StudentCatalogSingleton.Instance;
     CreateStudentHandler = new CreateStudentHandler(this);
     _addCommand          = new RelayCommand(CreateStudentHandler.AddStudent);
 }
Ejemplo n.º 2
0
        public async Task CreateStudent_InvalidStudent_Fails()
        {
            // Arrange
            var student = new CreateStudent("", "", DateTime.Now);

            studentRepository.Setup(s => s.Add(It.IsAny <Student>())).ReturnsAsync(1);
            var handler = new CreateStudentHandler(studentRepository.Object);

            // Act
            var result = await handler.Handle(student, CancellationToken.None);

            // Assert
            result.Match(
                Left: error => Assert.Pass(),
                Right: result => Assert.Fail("Validation should have failed"));
        }
Ejemplo n.º 3
0
        public async Task Handle_should_create_entity()
        {
            var repo = NSubstitute.Substitute.For <IStudentsRepository>();

            var unitOfWork = NSubstitute.Substitute.For <ISchoolUnitOfWork>();

            unitOfWork.StudentsRepository.ReturnsForAnyArgs(repo);

            var validator = NSubstitute.Substitute.For <IValidator <CreateStudent> >();

            validator.ValidateAsync(null, CancellationToken.None)
            .ReturnsForAnyArgs(ValidationResult.Successful);

            var sut = new CreateStudentHandler(validator, unitOfWork);

            var command = new CreateStudent(Guid.NewGuid(), "new", "student");
            await sut.Handle(command, CancellationToken.None);

            repo.Received(1).CreateAsync(Arg.Is((Student c) => c.Id == command.StudentId && c.Firstname == command.StudentFirstname && c.Lastname == command.StudentLastname), 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 CreateStudentHandler(new FakeValidator <CreateStudent>(), unitOfWork);

            var command = new CreateStudent(Guid.NewGuid(), "new", "student");
            await sut.Handle(command, CancellationToken.None);

            var createdStudent = await studentsRepo.FindByIdAsync(command.StudentId, CancellationToken.None);

            createdStudent.Should().NotBeNull();
            createdStudent.Id.Should().Be(command.StudentId);
            createdStudent.Firstname.Should().Be(command.StudentFirstname);
            createdStudent.Lastname.Should().Be(command.StudentLastname);
        }