Of course, one of the biggest advantages of MVC is its testability, and no overview of ASP.NET MVC would be complete without looking at this functionality. Note that this functionality is not available in all versions of Visual Studio. An alternative is to use an open source framework such as NUnit (which is arguably much better supported and documented, anyway).
To avoid accessing the database, you will create a fake class to return films:
Create a new folder in the project called Support.
Add a new class called FakeFilmRepository.
Replace the code in FakeFilmRepository.cs with the following:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Chapter13.BobsMoviesMVC.Models;
namespace BobsMoviesMVC.Support { public class FakeFilmRepository : IFilmRepository { public List<Film> Films = new List<Film>(); #region IFilmRepository Members public bool Add(Film film) { throw new NotImplementedException(); } public Film Create() { throw new NotImplementedException(); } public void Delete(int ID) { throw new NotImplementedException(); } public IEnumerable<Film> GetAll() { return Films; } public Film GetFilm(int FilmID) { return Films.Single(f => f.FilmID == FilmID); } public void Save() { throw new NotImplementedException(); } public bool Update(Film film) { throw new NotImplementedException(); } #endregion } }
When you first created the project, Visual Studio asked whether you wanted to create a test project. You should have selected Yes, so you should have two projects in your solution.
Open the project named Chapter13.BobsMoviesMVC.Test.
Right-click the Controllers folder, and select Add New Item Class. Call it FilmControllerTest.
Enter the following code for the test:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using Microsoft.VisualStudio.TestTools.UnitTesting; using BobsMoviesMVC; using Chapter13.BobsMoviesMVC.Controllers; using Chapter13.BobsMoviesMVC.Models; namespace BobsMoviesMVC.Tests.Controllers { [TestClass] public class FilmControllerTest { [TestMethod] public void All_Action_Should_Return_All_View() { FilmController controller = new FilmController(GetFakeRepository()); ViewResult result = controller.All() as ViewResult; Assert.IsNotNull(result); } [TestMethod] public void Test_To_Show_What_Failed_Test_Looks_Like() { Assert.IsTrue(1 == 2); } [TestMethod] public void Detail_Action_Should_Return_Specific_Film_Matching_ID() { FilmController controller = new FilmController(GetFakeRepository()); ViewResult result = controller.Detail(1) as ViewResult; Assert.IsTrue(result.ViewData["title"].ToString() == "Test film"); } [TestMethod] public void Film_Should_NotValidate_With_No_Title() { Film Film= new Film(); Film.Title = ""; Assert.IsTrue(Film.IsValid() == false); }
private BobsMoviesMVC.Support.FakeFilmRepository GetFakeRepository() { //Set up pretend repository BobsMoviesMVC.Support.FakeFilmRepository PretendRepository = new BobsMoviesMVC.Support.FakeFilmRepository(); //Add a film to pretend repository PretendRepository.Films.Add(new Film { FilmID = 1, Title = "Test film" }); return PretendRepository; } } }
You now need to tell the Film controller to use the FakeFilmReposiory. Open FilmController, and add the following constructor to allow you to inject the fake repository:
public FilmController(BobsMoviesMVC.Models.IFilmRepository Repository) { filmRepository = Repository; }
You can run tests in a number of ways. One way is to open the test window:
Select Test on the main menu; then choose Windows Test View.
Select the test Film_Should_NotValidate_With_No_Title.
Right-click it, and select "Run selection."
After the test has run, a dialog box indicating the current status will be displayed (see Figure 13-9).
3.147.193.141