Defining RedirectToHomePage in a RouterController

 Given a RouterController
 When RedirectToHomePage 
 Should Redirect the Response to HomePageUrl
 Should not Redirect if the OriginUrl was HomePageUrl

Test Code using Static Constructs

    [TestClass]
    public class Given_RouterController {

        readonly RouterController Subject = Create.TestObjectFor<RouterController>();

        [TestMethod]
        public void When_RedirectToHomePage_Should_redirect_Response_to_HomePageUrl() {
            
            Subject.OriginUrl = "unknown page";
            Subject.RedirectToHomePage();
            Then.Should(Subject).Response.Redirect(Settings.HomePageUrl);
        }

        [TestMethod]
        public void When_RedirectToHomePage_Should_not_Redirect_if_OriginUrl_is_HomePage() {

            Subject.OriginUrl = Settings.HomePageUrl;
            Subject.RedirectToHomePage();
            Then.ShouldNot(Subject).Response.Redirect(Settings.HomePageUrl);
        }
    }

Test Code using Extensions Constructs

    [TestClass]
    public class Given_RouterController {

        readonly RouterController Subject = Create.TestObjectFor<RouterController>();

        [TestMethod]
        public void When_RedirectToHomePage_Should_redirect_Response_to_HomePageUrl() {
            
            Subject.OriginUrl = "unknown page";
            Subject.RedirectToHomePage();
            Subject.Should().Response.Redirect(Settings.HomePageUrl);
        }

        [TestMethod]
        public void When_RedirectToHomePage_Should_not_Redirect_if_OriginUrl_is_HomePage() {

            Subject.OriginUrl = Settings.HomePageUrl;
            Subject.RedirectToHomePage();
            Subject.ShouldNot().Response.Redirect(Settings.HomePageUrl);
        }
    }

Production Code

    public class RouterController : Controller<RouterService, RouterView> {

        public string OriginUrl { get; set; }

        public void RedirectToHomePage() {
            if (OriginUrl.Equals(Settings.HomePageUrl)) return;
            Response.Redirect(Settings.HomePageUrl);
        }
    }

    public interface Response {

        void Redirect(string url);
    }

    public abstract class Controller<M, V> {
        public M Model { get; set; }
        public V View { get; set; }
        public Server Server { get; set; }
        public Request Request { get; set; }
        public Response Response { get; set; }
    }

Last edited Nov 5, 2008 at 1:55 AM by mikemps, version 2

Comments

No comments yet.