Testing Controllers in ASP.NET MVC
page 2 of 5
by Manning Publications
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 32327/ 89

Testing the RedirectController

The RedirectController must find the next conference and issue a redirect to another url so that a single conference can be displayed on the screen. This controller must find the conference and ask for a redirect to the action that can take it from there. The ASP.NET MVC Framework provides a redirect mechanism so that we need not use Response.Redirect() explicitly. In listing 1, we set up a unit test for this code along with some fake implementations of the dependencies on which the RedirectController relies.

Listing 1 – RedirectControllerTester: Ensuring we redirect to the correct url

using System; 
using System.Web.Mvc; 
using CodeCampServer.Core.Domain; 
using CodeCampServer.Core.Domain.Model;
using NUnit.Framework; 
using NUnit.Framework.SyntaxHelpers;
namespace MvcInAction.Controllers.UnitTests 
{ 
      [TestFixture] 
      public class RedirectControllerTester 
      {
            [Test] 
            public void ShouldRedirectToTheNextConference() 
            { 
                  //arrange the set up for the text 
                  var conferenceToFind = 
                        new Conference{Key = "thekey", Name = "name"}; 
                  var repository = new ConferenceRepositoryStub(conferenceToFind); 
 
                  //create class under test using simulated dependencies 
                  var controller = new RedirectController(repository); 
 
                  //act - exercise the class under test 
                  RedirectToRouteResult result = controller.NextConference(); 
 
                  //assert - ensure the operation did everything expected 
                  Assert.That(result.RouteValues["controller"],
                        Is.EqualTo("conference")); 
                  Assert.That(result.RouteValues["action"], Is.EqualTo("index")); 
                  Assert.That(result.RouteValues["conferenceKey"], 
                        Is.EqualTo("thekey")); 
            }
 
            private class ConferenceRepositoryStub : IConferenceRepository 
            { 
                  private readonly Conference _conference; 
 
                  public ConferenceRepositoryStub(Conference conference) 
                  { 
                        _conference = conference; 
                  }
 
                  public Conference GetNextConference() 
                  {
                        return _conference; 
                  }
 
                  public Conference[] GetAllForUserGroup(UserGroup usergroup) 
                  { 
                        throw new NotImplementedException(); 
                  }
 
                  public Conference[] GetFutureForUserGroup(UserGroup usergroup) 
                  { 
                        throw new NotImplementedException(); 
                  } 
 
                  public Conference GetById(Guid id) 
                  { 
                        throw new NotImplementedException(); 
                  } 
 
                  public void Save(Conference entity) 
                  {
                        throw new NotImplementedException(); 
                  }
 
                  public Conference[] GetAll() 
                  {
                        throw new NotImplementedException(); 
                  }
 
                  public void Delete(Conference entity) 
                  {
                        throw new NotImplementedException(); 
                  }
 
                  public Conference GetByKey(string key) 
                  {
                        throw new NotImplementedException(); 
                  }
            }
      }
}

Notice that most of the code listing is test double code, and not the RedirectController test itself. We have to stub out an IConferenceRepository implementation because calling that interface inside the controller action provides the conference that is next. How it performs that search is beyond the scope of this article and is irrelevant to the controller. When glancing at this test, you probably think that it is too complex for a single unit test. We will shortly see how to reduce the amount of code in the unit test fixture. It starts with making dependencies explicit.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-03-24 11:58:08 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search