Controllers
Validationの呼び出し変更に伴う修正を行いました。
... // // POST: /Dinners/Edit/5 [AcceptVerbs(HttpVerbs.Post), Authorize] public ActionResult Edit(int id, FormCollection collection) { Dinner dinner = dinnerRepository.GetDinner(id); if (!dinner.IsHostedBy(User.Identity.Name)) return View("InvalidOwner"); /* 修正前 try { UpdateModel(dinner); dinnerRepository.Save(); return RedirectToAction("Details", new { id=dinner.DinnerID }); } catch { ModelState.AddModelErrors(dinner.GetRuleViolations()); return View(new DinnerFormViewModel(dinner)); } */ // 修正後 try { UpdateModel(dinner); if (dinner.IsValid) { dinnerRepository.Save(); return RedirectToAction("Details", new { id=dinner.DinnerID }); } else { ModelState.AddModelErrors(dinner.GetRuleViolations()); } } catch (Exception ex) { ModelState.AddModelError("_FORM", ex.Message); } return View(new DinnerFormViewModel(dinner)); } // // GET: /Dinners/Create [Authorize] public ActionResult Create() { Dinner dinner = new Dinner() { EventDate = DateTime.Now.AddDays(7), Country = "JP" // 追加 }; return View(new DinnerFormViewModel(dinner)); } // // POST: /Dinners/Create [AcceptVerbs(HttpVerbs.Post), Authorize] public ActionResult Create(Dinner dinner) { /* 修正前 if (ModelState.IsValid) { try { dinner.HostedBy = User.Identity.Name; RSVP rsvp = new RSVP(); rsvp.AttendeeName = User.Identity.Name; dinner.RSVPs.Add(rsvp); dinnerRepository.Add(dinner); dinnerRepository.Save(); return RedirectToAction("Details", new { id=dinner.DinnerID }); } catch (Exception ex) { ModelState.AddModelErrors(dinner.GetRuleViolations()); } } */ // 修正後 dinner.HostedBy = User.Identity.Name; if (dinner.IsValid) { try { RSVP rsvp = new RSVP(); rsvp.AttendeeName = User.Identity.Name; dinner.RSVPs.Add(rsvp); dinnerRepository.Add(dinner); dinnerRepository.Save(); return RedirectToAction("Details", new { id=dinner.DinnerID }); } catch (Exception ex) { ModelState.AddModelError("_FORM", ex.Message); } } else { ModelState.AddModelErrors(dinner.GetRuleViolations()); } return View(new DinnerFormViewModel(dinner)); } ...
NerdDinnerへの修正は以上です。
Unit Test
Visual Studio Team Systemテストツールを使用して作成したUnit TestプロジェクトをNUnit Testプロジェクトに修正します。といっても、[参照]アセンブリ内の「Microsoft.VisualStudio.QualityTools.UnitTestFramework」を「nunit.framework」へ変更、および次のような機械的な修正を施すだけです。
using System; using System.Text; using System.Collections.Generic; using System.Linq; //using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework; // 追加 using NerdDinner.Controllers; using System.Web.Mvc; using NerdDinner.Models; using NerdDinner.Tests.Fakes; using Moq; using NerdDinner.Helpers; using System.Web.Routing; namespace NerdDinner.Tests.Controllers { //[TestClass] [TestFixture] public class DinnersControllerTest { DinnersController CreateDinnersController() { var testData = FakeDinnerData.CreateTestDinners(); var repository = new FakeDinnerRepository(testData); return new DinnersController(repository); } DinnersController CreateDinnersControllerAs(string userName) { var mock = new Mock<ControllerContext>(); mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns(userName); mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true); var controller = CreateDinnersController(); controller.ControllerContext = mock.Object; return controller; } //[TestMethod] [Test] public void DetailsAction_Should_Return_View_For_Dinner() { // Arrange var controller = CreateDinnersController(); // Act var result = controller.Details(1); // Assert //Assert.IsInstanceOfType(result, typeof(ViewResult)); Assert.IsInstanceOfType(typeof(ViewResult), result); } ...