Exemplo n.º 1
0
        public void Criar_RetornoUmViewResult_DeUmPost()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var controller = new PostsController(new ApplicationDbContext(options));
            var result     = controller.Criar();
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <Post>(viewResult.ViewData.Model);

            Assert.True(model != null);
            Assert.IsType <Post>(model);
        }
Exemplo n.º 2
0
        public async Task CriarPost_RetornaUmBadRequestResult_QuandoModelStateNaoValido()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var controller = new PostsController(new ApplicationDbContext(options));

            controller.ModelState.AddModelError("PostTitulo", "Required");

            var post = new Post
            {
                PostId = Guid.NewGuid()
            };

            var result = await controller.Criar(post);

            var badRequestResult = Assert.IsType <BadRequestObjectResult>(result);

            Assert.IsType <SerializableError>(badRequestResult.Value);
        }
Exemplo n.º 3
0
        public async Task CriarPost_RetornaUmRedirectEAdicionaPost_QuantoModelEhValido()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var controller = new PostsController(new ApplicationDbContext(options));
            var post       = new Post
            {
                PostId   = Guid.NewGuid(),
                Usuario  = new ApplicationUser(),
                Conteudo = "Conteúdo",
                Titulo   = "Título"
            };

            var result = await controller.Criar(post);

            var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Null(redirectToActionResult.ControllerName);
            Assert.Equal("Index", redirectToActionResult.ActionName);
        }