view Agendas/trunk/src/Agendas.Web.Tests/Controllers/EventoControllerTests.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 bdc946dd5c94
children d255b0621503
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.Linq;
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 EventoControllerTests: MvcControllerTestsBase
	{
        private void PopulatePersona()
        {
            using (new RequestEmulator(NhHelper.GetSessionFactory()))
            {
                var personaController = new PersonaController();
                personaController.Nueva(new PersonaNewModel
                                            {
                                                Nombre = "Persona 1",
                                                Blog = "http://blog.persona1.com",
                                                EMail = "info@persona1.com",
                                                Roles = new string[] {"Administrador", "Usuario"},
                                                Twitter = "persona1"
                                            });
            }
        }

	    private Guid GetGuidPersona()
        {
            using (new RequestEmulator(NhHelper.GetSessionFactory()))
            {
                var personaController = new PersonaController();
                var viewResult = (ViewResult) personaController.Index();
                return ((PersonaIndexModel) viewResult.Model).Items.FirstOrDefault().Id;
            }
        }

		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();
            PopulatePersona();
            SetearUsuario();
        }

        [Test]
        public void Nuevo_Evento()
        {
            ViewResult viewResult = null;
            IEnumerable<EventoDto> eventosDto = null;
            EventoPublicarModel eventoPublicar = null;
            var eventoController = new EventoController { ControllerContext = ControllerCtx };
            var form = new FormCollection
                           {
                               {"Duracion", (new TimeSpan(0, 0, 0)).ToString()},
                               {"Fecha", (DateTime.Today.AddDays(15)).ToString()},
                               {"Hora", (new TimeSpan(18, 0, 0)).ToString()},
                               {"Ponentes", GetGuidPersona().ToString()},
                               {"TipoEvento", ((int)TipoEvento.Van).ToString()},
                               {"Titulo", "Título"}
                           };
            var eventoNew = BindModel<EventoNewModel>(eventoController, form); 

            using (new RequestEmulator(NhHelper.GetSessionFactory()))
            {
                var resultNuevo = eventoController.Nuevo(eventoNew);
                Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultNuevo);
                Assert.AreEqual("Index", ((RedirectToRouteResult) resultNuevo).RouteValues["action"]);
            }
        }

        [Test]
        public void Nuevo_Evento_Erroneo_Sin_Ponente()
        {
            ViewResult viewResult = null;
            IEnumerable<EventoDto> eventosDto = null;
            EventoPublicarModel eventoPublicar = null;
            var eventoController = new EventoController { ControllerContext = ControllerCtx };
            var form = new FormCollection
                           {
                               {"Duracion", (new TimeSpan(0, 0, 0)).ToString()},
                               {"Fecha", (DateTime.Today.AddDays(15)).ToString()},
                               {"Hora", (new TimeSpan(18, 0, 0)).ToString()},
                               {"TipoEvento", ((int)TipoEvento.Van).ToString()},
                               {"Titulo", "Título"}
                           };
            var eventoNew = BindModel<EventoNewModel>(eventoController, form);

            using (new RequestEmulator(NhHelper.GetSessionFactory()))
            {
                var resultNuevo = (ViewResult)eventoController.Nuevo(eventoNew);
                var modelState = resultNuevo.ViewData.ModelState;
                Assert.AreEqual(1, modelState[string.Empty].Errors.Count);
                Assert.AreEqual("Debe indicar el ponente para este tipo de evento", modelState[string.Empty].Errors.FirstOrDefault().ErrorMessage);
            }
        }

	    [Test]
		public void Publicar_Evento_Erroneo_Duracion_Fuera_Rango()
		{
		    ViewResult viewResult = null;
		    IEnumerable<EventoDto> eventosDto = null;
		    EventoPublicarModel eventoPublicar = null;
            var eventoController = new EventoController { ControllerContext = ControllerCtx };
            var formEvento = new FormCollection
                           {
                               {"Duracion", (new TimeSpan(0, 0, 0)).ToString()},
                               {"Fecha", (DateTime.Today.AddDays(15)).ToString()},
                               {"Hora", (new TimeSpan(18, 0, 0)).ToString()},
                               {"Ponentes", GetGuidPersona().ToString()},
                               {"TipoEvento", ((int)TipoEvento.Van).ToString()},
                               {"Titulo", "Título 2"}
                           };
            var eventoNew = BindModel<EventoNewModel>(eventoController, formEvento); 
		    
			using (new RequestEmulator(NhHelper.GetSessionFactory()))
			{
			    eventoController.Nuevo(eventoNew);
			}
            using (new RequestEmulator(NhHelper.GetSessionFactory()))
            {
                viewResult = (ViewResult)eventoController.Index();
			}
            using (new RequestEmulator(NhHelper.GetSessionFactory()))
            {
                eventosDto = ((EventoIndexModel) viewResult.Model).ProximosEventos;
            }
            using (new RequestEmulator(NhHelper.GetSessionFactory()))
            {
                var firstEvent = eventosDto.LastOrDefault();
                var formPublicar = new FormCollection
                           {
                               {"Id", (firstEvent.Id).ToString()},
                               {"Titulo", "Título 2"},
                               {"NroOrden", "1"},
                               {"UrlWiki", "http://www.altnethistpano.org/vans/titulo-1.ashx"},
                               {"DuracionReal", (new TimeSpan(0, 0, 0)).ToString()}
                           };
                eventoPublicar = BindModel<EventoPublicarModel>(eventoController, formPublicar); 
            }
            using (new RequestEmulator(NhHelper.GetSessionFactory()))
            {
                var resultPublicar = (ViewResult)eventoController.Publicar(eventoPublicar);
                var modelState = resultPublicar.ViewData.ModelState;

                Assert.AreEqual(1, modelState[string.Empty].Errors.Count);
                Assert.AreEqual("Duración fuera de rango", modelState[string.Empty].Errors.FirstOrDefault().ErrorMessage);
                
            }
        }
	}

}