view Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs @ 264:cef4fc893333

EventoControllerTests: No se estaba pasando el HttpContext al test.
author juanjose.montesdeocaarbos
date Sat, 22 Oct 2011 17:27:45 -0300
parents 3339d1da6a63
children 17694f70cfb6
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Agendas.NHibernate;
using Agendas.Repositories.Tests.Infraestructure;
using AltNetHispano.Agendas.Domain;
using AltNetHispano.Agendas.Repositories.NHibernate;
using AltNetHispano.Agendas.Web.Controllers;
using AltNetHispano.Agendas.Web.Models;
using Moq;
using NUnit.Framework;

namespace Agendas.Web.Tests.Controllers
{
    [TestFixture]
    public class PersonaControllerTests : MvcControllerTestsBase
    {
        private void SetearUsuario()
        {
            var seguridad = new Mock<ISeguridad>();
            seguridad.Setup(s => s.GetUserName()).Returns("neluz");
            IdentityContext.Init(seguridad.Object, new PersonaRepository(NhHelper.GetSessionFactory()));
        }

        [SetUp]
        public void Setup()
        {
            NhHelperTest.CleanDb();
            SetearUsuario();
        }

        [Test]
        public void PersonaNueva_ErrorAlGuardarTwitterConArroba()
        {
            var personaController = new PersonaController { ControllerContext = ControllerCtx };

            var form = new FormCollection
                           {
                               {"Blog", "http://juanjose.montesdeocaarbos.com.ar/blog/"},
                               {"EMail", "juanjose.montesdeocaarbos gmail.com"},
                               {"Nombre", "Juan José Montes de Oca Arbós"},
                               {"Roles", "Administrador"},
                               {"Roles", "Usuario"},
                               {"Twitter", "@jjmoa"}
                           };
            var personaNew = BindModel<PersonaNewModel>(personaController, form);

            using (new RequestEmulator(NhHelper.GetSessionFactory()))
            {
                var resultNuevo = personaController.Nueva(personaNew);
                Assert.IsInstanceOf(typeof (ViewResult), resultNuevo);
                Assert.AreEqual("Defaulteditor", ((ViewResult) resultNuevo).ViewName);
                Assert.IsFalse(personaController.TempData.ContainsKey("error"));
            }
        }

        [Test]
        public void PersonaModificar_ErrorAlGuardarTwitterConArroba()
        {
            var personaController = new PersonaController {ControllerContext = ControllerCtx};

            #region PersonaNew
            var personaNew = new PersonaNewModel
                                 {
                                     Blog = "http://juanjose.montesdeocaarbos.com.ar/blog/",
                                     EMail = "juanjose.montesdeocaarbos@gmail.com",
                                     Nombre = "Juan José Montes de Oca Arbós",
                                     Roles = new string[] {"Administrador", "Usuario"},
                                     Twitter = "jjmoa"
                                 };

            using (new RequestEmulator(NhHelper.GetSessionFactory()))
            {
                personaController.Nueva(personaNew);
            }
            #endregion

            PersonaEditModel personaEdit = null;
            using (new RequestEmulator(NhHelper.GetSessionFactory()))
            {
                var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory());

                var form = new FormCollection
                               {
                                   {"Id", personaRepository.GetAll().FirstOrDefault().Id.ToString()},
                                   {"Blog", "http://juanjose.montesdeocaarbos.com.ar/blog/"},
                                   {"EMail", "juanjose.montesdeocaarbos gmail.com"},
                                   {"Nombre", "Juan José Montes de Oca Arbós"},
                                   {"Roles", "Administrador"},
                                   {"Roles", "Usuario"},
                                   {"Twitter", "@jjmoa"}
                               };
                personaEdit = BindModel<PersonaEditModel>(personaController, form);
            }
            
            using (new RequestEmulator(NhHelper.GetSessionFactory()))
            {
                var resultModificar = personaController.Modificar(personaEdit);
                Assert.IsInstanceOf(typeof (ViewResult), resultModificar);
                Assert.AreEqual("Defaulteditor", ((ViewResult) resultModificar).ViewName);
                Assert.IsFalse(personaController.TempData.ContainsKey("error"));
            }
        }
    }
}