public async Task RegisterSpeechWithValidModelStateReturnOk()
        {
            //Arrange
            RegisterSpeechCommandMessage registerSpeechCommandMessage = null;
            var moqRegisterSpeechUseCase = new Mock <IRegisterSpeechUseCase>();

            moqRegisterSpeechUseCase.Setup(x => x.Handle(It.IsAny <RegisterSpeechCommandMessage>()))
            .Returns(Task.CompletedTask)
            .Callback <RegisterSpeechCommandMessage>(x => registerSpeechCommandMessage = x);
            var moqUpdateSpeechUseCase = new Mock <IUpdateSpeechUseCase>();
            var speechForCreationDto   = new SpeechForCreationDto
            {
                Title       = "is simply dummy text of the printing",
                Description =
                    @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy
                                text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged",
                Type = "1",
                Url  = "http://myjpg.jpg",
            };

            var sut = new SpeechController(moqRegisterSpeechUseCase.Object, moqUpdateSpeechUseCase.Object);

            //Act
            IActionResult result = await sut.Post(speechForCreationDto);

            //Assert
            Assert.IsType <OkResult>(result);
            moqRegisterSpeechUseCase.Verify(x => x.Handle(It.IsAny <RegisterSpeechCommandMessage>()), Times.Once);
            Assert.Equal(speechForCreationDto.Title, registerSpeechCommandMessage.Title);
            Assert.Equal(speechForCreationDto.Description, registerSpeechCommandMessage.Description);
            Assert.Equal(speechForCreationDto.Type, registerSpeechCommandMessage.Type);
            Assert.Equal(speechForCreationDto.Url, registerSpeechCommandMessage.Url);
        }
示例#2
0
        public async Task <IActionResult> Post([FromBody] SpeechForCreationDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var command = new RegisterSpeechCommandMessage(dto.Title, dto.Description, dto.Url, dto.Type);

            await _registerSpeechUseCase.Handle(command);

            return(Ok());
        }
        public async Task RegisterSpeechUseCaseWithValidInputReturnSuccessTest()
        {
            //Arrange

            /* ------------ I will use UnitOfWork pattern, it will help me to treat aggregate roots
             *              as a unit for the purpose of data changes */
            Mock <IUnitOfWork> moqUnitOfWork = new Mock <IUnitOfWork>();

            moqUnitOfWork.Setup(m => m.Commit()).Verifiable();

            /* ------------ I will use repository pattern, aggregate roots are the only objects my
             *              code loads from the repository.*/
            Mock <ISpeechRepository> moqSpeechRepository = new Mock <ISpeechRepository>();

            moqSpeechRepository.Setup(m => m.CreateAsync(It.IsAny <Domain.SpeechAggregate.Speech>()))
            .Returns(Task.FromResult <ISpeechRepository>(null)).Verifiable();

            // ------------ I'm on the command side of CQRS pattern, so I don't need an output port
            // ------------ I need a command to regsiter a new speech
            var registerSpeechCommand = new RegisterSpeechCommandMessage(
                "Microservices getting started",
                "A Microservices from scratch online event Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took rem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
                "http://microservices-getting-started.logcorner.com",
                "2");

            var mockEventSourcingSubscriber = new Mock <IEventSourcingSubscriber>();
            //Act
            // ------------ RegisterSpeechUseCase is the object under test
            IRegisterSpeechUseCase usecase =
                new RegisterSpeechUseCase(moqUnitOfWork.Object, moqSpeechRepository.Object, mockEventSourcingSubscriber.Object);

            await usecase.Handle(registerSpeechCommand);

            //Assert

            /* ------------ The object returns void , so I will verify that a new Speech will be inserted into the database
             *              when SaveChanges is called.*/

            moqSpeechRepository.Verify(m => m.CreateAsync(It.IsAny <Domain.SpeechAggregate.Speech>()), Times.Once,
                                       "CreateAsync must be called only once");

            // Verify that SaveChanges is called
            moqUnitOfWork.Verify(m => m.Commit(), Times.Once, "Commit must be called only once");
        }
        public async Task RegisterSpeechUseCaseSuccessTest()
        {
            //Arrange
            //Use repository pattern to aggregate roots that are the only objects to be loaded from the repository

            Mock <ISpeechRepository> moqSpeechRepository = new Mock <ISpeechRepository>();

            moqSpeechRepository.Setup(m => m.CreateAsync(It.IsAny <Domain.SpeechAggregate.Speech>())).Returns(Task.FromResult <ISpeechRepository>(null)).Verifiable();

            /* Using UnitOFWork pattern, helps t treat aggregate rootsa as a unit for the purpose of datachanges */

            var moqUnitOfWork = new Mock <IUnitOfWork>();

            moqUnitOfWork.Setup(m => m.Commit()).Verifiable();


            //Command side of the CQRS pattern, no need for an output port
            //Register a new speech

            var registerSpeechCommand = new RegisterSpeechCommandMessage(
                "Microservices getting started",
                "A microservices from scratch online event",
                "https://github.com/Dontunee/BuildingMicroservicesThroughEventDrivenArchitecture",
                "2"
                );



            //Act
            //RegisterSpeechUseCase is the object under test
            IRegisterSpeechUseCase registerSpeechUseCase = new RegisterSpeechUseCase(moqUnitOfWork.Object, moqSpeechRepository.Object);

            await registerSpeechUseCase.Handle(registerSpeechCommand);

            //Assert
            //Verify that a new speech will be inserted into the database when savechanges is called

            // var data = new Mock<Domain.SpeechAggregate.Speech>();
            //moqSpeechRepository.Verify(m => m.CreateAsync(data.Object), Times.Once, "Create async must be called only once");

            //Verify that savechanges is called
            moqUnitOfWork.Verify(m => m.Commit(), Times.Once, "Commit must be called only once");
        }