diff Agendas/trunk/src/Agendas.Web.Tests/Controllers/PatrocinadorControllerTests.cs @ 278:6f5ab71614d4

#125: ABM de patrocinadores.
author juanjose.montesdeocaarbos
date Wed, 21 Dec 2011 08:47:05 -0300
parents bf993f99cee3
children 1e889a2e45c5
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PatrocinadorControllerTests.cs	Tue Dec 20 08:28:33 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PatrocinadorControllerTests.cs	Wed Dec 21 08:47:05 2011 -0300
@@ -9,6 +9,7 @@
 using Agendas.NHibernate;
 using Agendas.Repositories.Tests.Infraestructure;
 using Agendas.Web.Tests.Helpers;
+using AltNetHispano.Agendas.Repositories.NHibernate;
 using AltNetHispano.Agendas.Web.Controllers;
 using AltNetHispano.Agendas.Web.Models;
 using Moq;
@@ -20,12 +21,37 @@
 	[TestFixture]
 	public class PatrocinadorControllerTests : MvcControllerTestsBase
 	{
-		[SetUp]
-		public void RegisterBinders()
+		internal void CrearPatrocinadorApress()
+		{
+			var patrocinadorController = new PatrocinadorController(new HttpServerStub())
+			{
+				ControllerContext = ControllerCtx
+			};
+			var form = new FormCollection
+			           	{
+			           		{"Nombre", "APRESS"},
+			           		{"Logo", @"images\apress.gif"}
+			           	};
+			var patrocinadorNew = BindModel<PatrocinadorNewModel>(patrocinadorController, form);
+
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var resultNuevo = patrocinadorController.Nuevo(patrocinadorNew);
+			}
+		}
+
+		internal void RegisterBinders()
 		{
 			ModelBinders.Binders[typeof(HttpPostedFileBase)] = new HttpPostedFileBaseModelBinder();
 		}
 
+		[SetUp]
+		public void SetUpTests()
+		{
+			NhHelperTest.CleanDb();
+			RegisterBinders();
+		}
+
 		[Test]
 		public void PatrocinadorIndexGet()
 		{
@@ -98,7 +124,7 @@
 				var resultNuevo = patrocinadorController.Nuevo(patrocinadorNew);
 				Assert.IsInstanceOf(typeof(ViewResult), resultNuevo);
 				var viewResultNuevo = (ViewResult)resultNuevo;
-				Assert.AreEqual("DefaultEditor", viewResultNuevo.ViewName); //UploadFileEditor
+				Assert.AreEqual("DefaultEditor", viewResultNuevo.ViewName);
 				Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Nombre"].Errors.Count);
 				Assert.AreEqual("El campo Nombre es obligatorio.", viewResultNuevo.ViewData.ModelState["Nombre"].Errors[0].ErrorMessage);
 				Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Logo"].Errors.Count);
@@ -107,11 +133,125 @@
 		}
 
 		[Test]
-		[Ignore]
-		public void PatrocinadorNuevoPostExistente()
+		public void PatrocinadorNuevoPostExists()
+		{
+			CrearPatrocinadorApress();
+
+			var patrocinadorController = new PatrocinadorController(new HttpServerStub())
+			                             	{
+			                             		ControllerContext = ControllerCtx
+			                             	};
+			var form = new FormCollection
+			           	{
+			           		{"Nombre", "APRESS"},
+			           		{"Logo", @"images\apress.gif"}
+			           	};
+			var patrocinadorNew = BindModel<PatrocinadorNewModel>(patrocinadorController, form);
+
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var resultNuevo = patrocinadorController.Nuevo(patrocinadorNew);
+				Assert.IsInstanceOf(typeof (ViewResult), resultNuevo);
+				var viewResultNuevo = (ViewResult) resultNuevo;
+
+				Assert.AreEqual("El patrocinador ya existe.", viewResultNuevo.TempData["error"]);
+			}
+		}
+
+		[Test]
+		public void PatrocinadorModificarGetDontExists()
+		{
+			var patrocinadorController = new PatrocinadorController(new HttpServerStub())
+			{
+				ControllerContext = ControllerCtx
+			};
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var resultModificar = patrocinadorController.Modificar("11111111-1111-1111-1111-111111111111");
+				Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultModificar);
+				Assert.AreEqual("Index", ((RedirectToRouteResult)resultModificar).RouteValues["action"]);
+				Assert.AreEqual("No se encontrĂ³ el patrocinador que intenta modificar", patrocinadorController.TempData["error"]);
+			}
+		}
+
+		[Test]
+		public void PatrocinadorModificarGetExists()
 		{
+			CrearPatrocinadorApress();
 
+			var patrocinadorController = new PatrocinadorController(new HttpServerStub())
+			{
+				ControllerContext = ControllerCtx
+			};
+
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var patrocinadorRepository = new PatrocinadorRepository(NhHelper.GetSessionFactory());
+				var resultModificar = patrocinadorController.Modificar(patrocinadorRepository.GetAll().FirstOrDefault().Id.ToString());
+				Assert.IsInstanceOf(typeof(ViewResult), resultModificar);
+				Assert.AreEqual("DefaultEditor", ((ViewResult)resultModificar).ViewName);
+			}
+		}
+
+		[Test]
+		public void PatrocinadorModificarPostSatisfactorio()
+		{
+			PatrocinadorEditModel patrocinadorEdit;
+
+			CrearPatrocinadorApress();
+			var patrocinadorController = new PatrocinadorController(new HttpServerStub())
+			                             	{
+			                             		ControllerContext = ControllerCtx
+			                             	};
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var patrocinadorRepository = new PatrocinadorRepository(NhHelper.GetSessionFactory());
+
+				var form = new FormCollection
+				           	{
+				           		{"Id", patrocinadorRepository.GetAll().FirstOrDefault().Id.ToString()},
+				           		{"Nombre", "APRESS"},
+				           		{"Logo", @"images\apress.gif"}
+				           	};
+				patrocinadorEdit = BindModel<PatrocinadorEditModel>(patrocinadorController, form);
+			}
+
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var resultModificar = patrocinadorController.Modificar(patrocinadorEdit);
+				Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultModificar);
+				var viewResultEdit = (RedirectToRouteResult)resultModificar;
+				Assert.AreEqual("Index", viewResultEdit.RouteValues["action"]);
+			}
+		}
+
+		[Test]
+		public void PatrocinadorModificarPostRequiredFields()
+		{
+			CrearPatrocinadorApress();
+			var patrocinadorController = new PatrocinadorController(new HttpServerStub())
+			{
+				ControllerContext = ControllerCtx
+			};
+			var form = new FormCollection
+				        {
+				           	{"Id", ""},
+				           	{"Nombre", ""},
+				           	{"Logo", ""}
+				        };
+			var patrocinadorEdit = BindModel<PatrocinadorEditModel>(patrocinadorController, form);
+
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var resultModificar = patrocinadorController.Modificar(patrocinadorEdit);
+				Assert.IsInstanceOf(typeof(ViewResult), resultModificar);
+				var viewResultNuevo = (ViewResult)resultModificar;
+				Assert.AreEqual("DefaultEditor", viewResultNuevo.ViewName);
+				Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Nombre"].Errors.Count);
+				Assert.AreEqual("El campo Nombre es obligatorio.", viewResultNuevo.ViewData.ModelState["Nombre"].Errors[0].ErrorMessage);
+				Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Logo"].Errors.Count);
+				Assert.AreEqual("El campo Logo es obligatorio.", viewResultNuevo.ViewData.ModelState["Logo"].Errors[0].ErrorMessage);
+			}
 		}
 	}
-
 }