13.13. Testing

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).

13.13.1. Creating a Fake Film Repository

To avoid accessing the database, you will create a fake class to return films:

  1. Create a new folder in the project called Support.

  2. Add a new class called FakeFilmRepository.

  3. 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
        }
    }

13.13.2. Creating a Test

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.

  1. Open the project named Chapter13.BobsMoviesMVC.Test.

  2. Right-click the Controllers folder, and select Add New Item Class. Call it FilmControllerTest.

  3. 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;
            }
        }
    }

13.13.3. Modify the Film Controller

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;
}

13.13.4. Running Tests

You can run tests in a number of ways. One way is to open the test window:

  1. Select Test on the main menu; then choose Windows Test View.

  2. Select the test Film_Should_NotValidate_With_No_Title.

  3. 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).

Figure 13.9. Visual Studio Test Results dialog box

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.141.75