Ejemplo n.º 1
0
        public void PostService_Crear_Test_Success()
        {
            // Arrange
            // Fake Variable
            var post = new Post()
            {
                Autor    = "Author",
                Activo   = true,
                Titulo   = "Titulo_Test",
                Texto    = "Test",
                PostType = PostType.Post
            };
            // Fake a Context
            IPostDemoContext context = A.Fake <IPostDemoContext>();

            A.CallTo(() => context.GuardarPost(post)).Returns(post);

            // A Real Service with a Fake Dependence
            var postService = new PostService(context);

            // Act
            var resultado = postService.Crear(post);

            // Assert
            Assert.AreEqual("Author", resultado.Autor);
            Assert.AreEqual(true, resultado.Activo);
            Assert.AreEqual("Titulo_Test", resultado.Titulo);
            Assert.AreEqual("Test", resultado.Texto);
            Assert.AreEqual(PostType.Post, resultado.PostType);
            Assert.IsNotNull(resultado.PostId);
            A.CallTo(() => context.GuardarPost(post)).MustHaveHappened();
        }
Ejemplo n.º 2
0
        public void PostService_Crear_Test_Failed()
        {
            // Arrange
            // Fake Variable
            var post = new Post()
            {
                Activo   = true,
                Titulo   = "Titulo_Test",
                Texto    = "Test",
                PostType = PostType.Post
            };
            // Fake a Context
            IPostDemoContext context = A.Fake <IPostDemoContext>();

            A.CallTo(() => context.GuardarPost(post)).Returns(post);

            // A Real Service with a Fake Dependence
            var postService = new PostService(context);

            // Act
            try
            {
                var resultado = postService.Crear(post);
                Assert.IsNull(resultado);
            }
            catch (Exception ex)
            {
                // Assert
                Assert.IsInstanceOfType(ex, typeof(Exception));
            }
        }
Ejemplo n.º 3
0
 public ActionResult Crear(PostCreateView postCreateView)
 {
     if (ModelState.IsValid)
     {
         try
         {
             var post = new Post()
             {
                 //Autor = User.Identity.Name,
                 Autor    = "Moises",
                 Texto    = postCreateView.Texto,
                 Titulo   = postCreateView.Titulo,
                 PostType = postCreateView.Tipo
             };
             var resultado = service.Crear(post);
             ViewBag.PostCreated = resultado;
             var mensaje = $"El post '{resultado.Titulo}' se creo correctamente";
             return(RedirectToAction("VerTodos", new { mensaje = mensaje }));
         }
         catch (Exception exception)
         {
             return(View("Error", exception));
         }
     }
     return(View(postCreateView));
 }