示例#1
0
        public async Task Deve_Buscar_Registro_Por_Id_Pelo_Servico()
        {
            //Given
            InsertTestViewModel model = new InsertTestViewModel();

            model.Nome    = "Weslley Carneiro";
            model.Contato = "*****@*****.**";

            var services = new ServiceCollection();

            services.AddLogging();
            services.AddAutoMapper(typeof(TestViewModel));
            services.AddContext <TestContext>(new ContextOptionsBuilder());
            services.AddApplicationServices <ITestServiceApplication, TestServiceApplication>("Optsol.Components.Test.Utils");

            var provider = services.BuildServiceProvider();
            ITestServiceApplication serviceApplication = provider.GetRequiredService <ITestServiceApplication>();

            //When
            await serviceApplication.InsertAsync(model);

            var list = await serviceApplication.GetAllAsync();

            var modelResult = await serviceApplication.GetByIdAsync(list.DataList.Single().Id);

            //Then
            modelResult.Invalid.Should().BeFalse();
            modelResult.Notifications.Should().HaveCount(0);

            modelResult.Should().NotBeNull();
            modelResult.Data.Nome.Should().Be(model.Nome);
            modelResult.Data.Contato.Should().Be(model.Contato);
        }
        public async Task Deve_Atualizar_Registro_Pelo_Servico()
        {
            //Given
            InsertTestViewModel model = new InsertTestViewModel();

            model.Nome    = "Weslley Carneiro";
            model.Contato = "*****@*****.**";

            var services = new ServiceCollection();

            services.AddLogging();
            services.AddAutoMapper(typeof(TestViewModel));
            services.AddContext <TestContext>(new ContextOptionsBuilder());
            services.AddApplicationServices <ITestServiceApplication, TestServiceApplication>("Optsol.Components.Test.Utils");
            services.AddAServices();

            var provider = services.BuildServiceProvider();
            ITestServiceApplication serviceApplication = provider.GetRequiredService <ITestServiceApplication>();
            IApiControllerBase <TestEntity, TestViewModel, TestViewModel, InsertTestViewModel, UpdateTestViewModel> controllerBase =
                new ApiControllerBase <TestEntity, TestViewModel, TestViewModel, InsertTestViewModel, UpdateTestViewModel>(
                    provider.GetRequiredService <ILogger <ApiControllerBase <TestEntity, TestViewModel, TestViewModel, InsertTestViewModel, UpdateTestViewModel> > >(),
                    provider.GetRequiredService <IBaseServiceApplication <TestEntity, TestViewModel, TestViewModel, InsertTestViewModel, UpdateTestViewModel> >(),
                    provider.GetRequiredService <IResponseFactory>());

            await serviceApplication.InsertAsync(model);

            var data        = (await serviceApplication.GetAllAsync()).DataList.Single();
            var updateModel = new UpdateTestViewModel();

            updateModel.Id      = data.Id;
            updateModel.Nome    = $"Weslley Alterado";
            updateModel.Contato = model.Contato;

            //When
            var actionResult = await controllerBase.UpdateAsync(updateModel);

            //Then
            ((OkObjectResult)actionResult).StatusCode.Should().NotBeNull();
            ((OkObjectResult)actionResult).StatusCode.Should().Be((int)HttpStatusCode.OK);

            var resultObj = JsonConvert.DeserializeObject <Response>(((OkObjectResult)actionResult).Value.ToJson());

            resultObj.Should().NotBeNull();
            resultObj.Success.Should().BeTrue();
            resultObj.Failure.Should().BeFalse();
            resultObj.Messages.Should().BeEmpty();

            var resultService = await serviceApplication.GetByIdAsync(updateModel.Id);

            resultService.Data.Should().NotBeNull();
            resultService.Data.Id.Should().NotBeEmpty();
            resultService.Data.Nome.Should().Be(updateModel.Nome);
            resultService.Data.Contato.Should().Be(updateModel.Contato);
            resultService.Data.Ativo.Should().Be("Inativo");
        }