public async Task <IActionResult> RemoveStudent([FromBody] RemoveStudentCommand removeStudentCommand) { await _courseService.RemoveStudentFromCourseAsync(removeStudentCommand.CourseId, removeStudentCommand.StudentId); return(Ok()); }
public async Task <IActionResult> Delete(int id) { var removeStudentCommand = new RemoveStudentCommand(id); var response = await _mediator.Send(removeStudentCommand); return(Ok(response)); }
public void SuccesfullyRemoveStudent() { // Arrange string firstName = "pesho"; string lastName = "peshov"; string grade = "7"; IList <string> args = new List <string>() { firstName, lastName, grade }; var studentFactory = new StudentFactory(); var createStudentCommand = new CreateStudentCommand(studentFactory); var removeStudentCommand = new RemoveStudentCommand(); // Act var createdStudent = createStudentCommand.Execute(args); int studentsBeforeRemove = Engine.Students.Count; removeStudentCommand.Execute(new List <string>() { "0" }); int studentsAfterRemove = Engine.Students.Count; // Assert Assert.AreEqual(studentsBeforeRemove - 1, studentsAfterRemove); }
public void ConstructorWithParametersShouldSetCorrect() { Mock <ISchoolRegister> mockedSchoolRegister = new Mock <ISchoolRegister>(); RemoveStudentCommand removeStudentCommand = new RemoveStudentCommand(mockedSchoolRegister.Object); Assert.IsInstanceOf <RemoveStudentCommand>(removeStudentCommand); }
public async Task RemoveAsync(Guid id) { var removeCommand = new RemoveStudentCommand(id) { User = "******" }; await _bus.SendCommand(removeCommand); }
public void NotToThrow_IfPassedParameterIsValid() { var mockedEngine = new Mock <IEngine>(); var command = new RemoveStudentCommand(mockedEngine.Object); Assert.AreEqual(typeof(RemoveStudentCommand), command.GetType()); }
public async Task DeleteStudent(int studentId, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var command = new RemoveStudentCommand(studentId); await _mediator.Send(command, cancellationToken); }
public void TestExecute_PassValidId_ShouldCallServiceRemoveStudentCorrectly(int id) { var parameters = new string[] { id.ToString() }; var mockedService = new Mock <ISchoolService>(); var command = new RemoveStudentCommand(mockedService.Object); command.Execute(parameters); mockedService.Verify(s => s.RemoveStudent(id), Times.Once); }
public void CallSchoolSystemDataWithCorrectId_WhenPassedArgumentAreCorrect() { // arrange var schoolSystemData = new Mock <ISchoolSystemData>(); var removeStudentCommand = new RemoveStudentCommand(schoolSystemData.Object); // act removeStudentCommand.Execute(this.parameters); // assert schoolSystemData.Verify(x => x.RemoveStudent(5), Times.Once); }
public void TestExecute_PassValidId_ShouldCallServiceRemoveStudentCorrectly(int id) { var parameters = new string[] { id.ToString() }; var mockedService = new Mock<ISchoolService>(); var command = new RemoveStudentCommand(mockedService.Object); command.Execute(parameters); mockedService.Verify(s => s.RemoveStudent(id), Times.Once); }
public void RemoveStudentWithCorrectId_WhenParametersAreCorrect(IList <string> parameters) { // Arrange var studentId = int.Parse(parameters[0]); var mockRemoveStudent = new Mock <IRemoveStudent>(); var removeStudentCommand = new RemoveStudentCommand(mockRemoveStudent.Object); // Act removeStudentCommand.Execute(parameters); // Assert mockRemoveStudent.Verify(a => a.RemoveStudent(studentId), Times.Once()); }
public void Execute_ShouldThrowFormatException_WhenInvalidArgumentsArePassed() { //Arrange var parameters = new List <string>() { "I'm not a valid number!" }; var mockEngine = new Mock <ISchoolSystemEngine>(); var command = new RemoveStudentCommand(); //Assert & Act Assert.Throws <FormatException>(() => command.Execute(parameters, mockEngine.Object.Data)); }
public void ReturnCorrectMessage_WhenStudentHasBeenRemovedSuccessfully() { // arrange var schoolSystemData = new Mock <ISchoolSystemData>(); var removeStudentCommand = new RemoveStudentCommand(schoolSystemData.Object); string expectedMessage = "Student with ID 5 was sucessfully removed."; // act string actualMessage = removeStudentCommand.Execute(this.parameters); // assert StringAssert.Contains(expectedMessage, actualMessage); }
public void ReturnSuccessMessage_WhenParametersAreCorrect(IList <string> parameters) { // Arrange var studentId = int.Parse(parameters[0]); var mockRemoveStudent = new Mock <IRemoveStudent>(); var expectedResult = string.Format(SuccessMessageTemplate, studentId); var removeStudentCommand = new RemoveStudentCommand(mockRemoveStudent.Object); // Act var result = removeStudentCommand.Execute(parameters); // Assert Assert.AreEqual(expectedResult, result); }
public void Execute_ShouldThrowArgumentNull_WhenNullArgumentsArePassed() { //Arrange var parameters = new List <string>() { null }; var mockEngine = new Mock <ISchoolSystemEngine>(); var command = new RemoveStudentCommand(); //Assert & Act Assert.Throws <ArgumentNullException>(() => command.Execute(parameters, mockEngine.Object.Data)); }
public void ReturnCorrectMessage() { // Arrange var mockedEngine = new Mock <IEngine>(); var command = new RemoveStudentCommand(mockedEngine.Object); mockedEngine.Setup(x => x.Students.Remove(It.IsAny <int>())); var parameters = new string[] { "1" }; var expectedMessage = "Student with ID 1 was sucessfully removed."; // Act var message = command.Execute(parameters); // Asser Assert.AreEqual(expectedMessage, message); }
public void TryToRemoveStudent_WithThisId() { // Arrange var mockedEngine = new Mock <IEngine>(); var command = new RemoveStudentCommand(mockedEngine.Object); mockedEngine.Setup(x => x.Students.Remove(It.IsAny <int>())).Verifiable(); // Act var parameters = new string[] { "1" }; command.Execute(parameters); // Assert mockedEngine.Verify(x => x.Students.Remove(It.IsAny <int>()), Times.Exactly(1)); }
public void Execute_ShouldThrow_WhenParameterCannotBeParsedToInt() { var mockSchoolSystemData = new Mock <ISchoolSystemData>(); mockSchoolSystemData.SetupGet(ssd => ssd.Students).Returns(new Mock <ISchoolSystemDataCollection <IStudent> >().Object); mockSchoolSystemData.SetupGet(ssd => ssd.Teachers).Returns(new Mock <ISchoolSystemDataCollection <ITeacher> >().Object); var parameters = new List <string>() { "forty-two" }; var removeStudentCommand = new RemoveStudentCommand(); Assert.That( () => removeStudentCommand.Execute(parameters, mockSchoolSystemData.Object), Throws.InstanceOf <FormatException>()); }
public Task <bool> Handle(RemoveStudentCommand request, CancellationToken cancellationToken) { if (!request.IsValid()) { NotifyValidationErrors(request); return(Task.FromResult(false)); } _studentRepository.Remove(request.Id); if (Commit()) { _bus.RaiseEvent(new StudentRemovedEvent(request.Id)); } return(Task.FromResult(true)); }
public Task <bool> Handle(RemoveStudentCommand message, CancellationToken cancellationToken) { if (!message.IsValid()) { NotifyValidationErrors(message); return(Task.FromResult(false)); } _studentRepository.Remove(message.Id); if (Commit()) { Bus.RaiseEvent(new StudentRemovedEvent(message.Id)); } return(Task.FromResult(true)); }
public void Execute_ShouldCallEngineRemoveStudentWithCorrectParameters_WhenValidArgumentsArePassed(string id) { //Arrange var parameters = new List <string>() { id }; var mockEngine = new Mock <ISchoolSystemEngine>(); mockEngine.Setup(e => e.Data.Students.Remove(int.Parse(id))); var command = new RemoveStudentCommand(); //Act command.Execute(parameters, mockEngine.Object.Data); //Assert mockEngine.Verify(e => e.Data.Students.Remove(int.Parse(id)), Times.Once()); }
/// <summary> /// 同上,RemoveStudentCommand 的处理方法 /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public Task <Unit> Handle(RemoveStudentCommand request, CancellationToken cancellationToken) { //命令验证 if (!request.IsValid()) { //错误信息收集 NotifyValidationErrors(request); return(Task.FromResult(new Unit())); } _writeStudentRepository.Remove(request.Id); //统一提交 if (Commit()) { // 提交成功后,这里需要发布领域事件 // 比如欢迎用户注册邮件呀,短信呀等 // waiting.... _bus.RaiseEvent(new StudentRemoveEvent(request.Id, request.User)); } return(Task.FromResult(new Unit())); }
public void Execute_ShouldInvoke_SchoolSystemDataStudentsRemoveMethodWithCorrectId() { var mockSchoolSystemData = new Mock <ISchoolSystemData>(); var mockStudentsDataCollection = new Mock <ISchoolSystemDataCollection <IStudent> >(); mockSchoolSystemData.SetupGet(ssd => ssd.Students).Returns(mockStudentsDataCollection.Object); mockSchoolSystemData.SetupGet(ssd => ssd.Teachers).Returns(new Mock <ISchoolSystemDataCollection <ITeacher> >().Object); var parameters = new List <string>() { "42" }; var removeStudentCommand = new RemoveStudentCommand(); removeStudentCommand.Execute(parameters, mockSchoolSystemData.Object); mockStudentsDataCollection.Verify(ssd => ssd.Remove(42), Times.Once()); }
public void Execute_ShouldReturnCorrectString() { var mockSchoolSystemData = new Mock <ISchoolSystemData>(); mockSchoolSystemData.SetupGet(ssd => ssd.Students).Returns(new Mock <ISchoolSystemDataCollection <IStudent> >().Object); mockSchoolSystemData.SetupGet(ssd => ssd.Teachers).Returns(new Mock <ISchoolSystemDataCollection <ITeacher> >().Object); var parameters = new List <string>() { "42" }; var removeStudentCommand = new RemoveStudentCommand(); var expectedString = $"Student with ID {"42"} was sucessfully removed."; var actualString = removeStudentCommand.Execute(parameters, mockSchoolSystemData.Object); Assert.True(actualString == expectedString); }
public ICommandResult Handle(RemoveStudentCommand command) { var student = _studentRepository.GetById(command.Id); AddNotifications(new Contract() .Requires() .IsNotNull(student, "student", "O aluno não existe")); if (Invalid) { return(new CommandResult(false, "Não foi possível remover o aluno", Notifications)); } _studentRepository.Remove(student); return(new CommandResult(true, "Aluno removido com sucesso", null)); }
public void Return_CorrectStringValue() { var parameters = new List <string>() { "0" }; var mockedStorage = new Mock <IStorage>(); mockedStorage.Setup(x => x.Students).Returns(new Dictionary <int, IStudent>() { { 0, It.IsAny <IStudent>() } }); var createStudentCommand = new RemoveStudentCommand(mockedStorage.Object); string expected = $"Student with ID {0} was sucessfully removed."; var actual = createStudentCommand.Execute(parameters); Assert.AreEqual(expected, actual); }
public CommandResult Handle(RemoveStudentCommand command) { var result = _studentRepository.GetById(command.Id); var name = new Name(result.FirstName, result.LastName); var address = new Address(result.Country, result.State, result.City, result.Neighborhood, result.Street, result.StreetNumber, result.Building); var email = new Email(result.Email); var student = new Student(command.Id, name, address, email, result.Phone); _studentRepository.Remove(student); if (_uow.Commit()) { return(new CommandResult(true, "Estudante removido com sucesso!")); } return(new CommandResult(false, "Erro ao tentar remover o aluno!")); }
public void RemoveStudent_InStorageCollection() { var parameters = new List <string>() { "0" }; var mockedStorage = new Mock <IStorage>(); mockedStorage.Setup(x => x.Students).Returns(new Dictionary <int, IStudent>() { { 0, It.IsAny <IStudent>() } }); var removeStudentCommand = new RemoveStudentCommand(mockedStorage.Object); var expected = mockedStorage.Object.Students.Count - 1; removeStudentCommand.Execute(parameters); Assert.AreEqual(expected, mockedStorage.Object.Students.Count); }
public void RemoveTheStudent_WithTheGivenId() { // Arrange string studentId = "0"; IList <string> parameters = new List <string>() { studentId }; var removeStudentMock = new Mock <IRemoveStudent>(); removeStudentMock.Setup(rs => rs.RemoveStudent(int.Parse(studentId))); ICommand removeStudent = new RemoveStudentCommand(removeStudentMock.Object); // Act removeStudent.Execute(parameters); // Assert removeStudentMock.Verify(rs => rs.RemoveStudent(int.Parse(studentId)), Times.Once()); }
public void MethodExecuteWithCorectParametersShouldRemoveStudentFromRegister() { IList <string> studentId = new List <string>() { "1" }; Mock <IStudent> mockedStudent = new Mock <IStudent>(); mockedStudent.Setup(x => x.FirstName).Returns("Pesho"); mockedStudent.Setup(x => x.LastName).Returns("Petrov"); mockedStudent.Setup(x => x.Grade).Returns(Grade.Eighth); ISchoolRegister schoolRegister = new SchoolRegister(); schoolRegister.Students.Add(int.Parse(studentId[0]), mockedStudent.Object); RemoveStudentCommand removeStudentCommand = new RemoveStudentCommand(schoolRegister); removeStudentCommand.Execute(studentId); Assert.AreEqual(0, schoolRegister.Students.Count); }
public void Execute_ShouldReturnCorrectValue_WhenValidArgumentsArePassed() { //Arrange const string ExpectedReturnValue = "Student with ID 1 was sucessfully removed."; var parameters = new List <string>() { "1" }; var mockEngine = new Mock <ISchoolSystemEngine>(); mockEngine.Setup(e => e.Data.Students.Remove(1)); var command = new RemoveStudentCommand(); //Act var result = command.Execute(parameters, mockEngine.Object.Data); var secondResult = command.Execute(parameters, mockEngine.Object.Data); //Assert Assert.AreEqual(ExpectedReturnValue, result); Assert.AreEqual(ExpectedReturnValue, secondResult); }