How to unit test this simple ASP.NET MVC controller

Posted by Frank Schwieterman on Stack Overflow See other posts from Stack Overflow or by Frank Schwieterman
Published on 2010-04-29T07:36:42Z Indexed on 2010/04/29 12:17 UTC
Read the original article Hit count: 357

Filed under:
|

Lets say I have a simple controller for ASP.NET MVC I want to test. I want to test that a controller action (Foo, in this case) simply returns a link to another action (Bar, in this case).

How would you test this? (either the first or second link)

My implementation has the same link twice. One passes the url throw ViewData[]. This seems more testable to me, as I can check the ViewData collection returned from Foo(). Even this way though, I don't know how to validate the url itself without making dependencies on routing.

The controller:

public class TestController : Controller
{
    public ActionResult Foo()
    {
        ViewData["Link2"] = Url.Action("Bar");
        return View("Foo");
    }

    public ActionResult Bar()
    {
        return View("Bar");
    }

}

the "Foo" view:

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" MasterPageFile="~/Views/Shared/Site.Master"%>

<asp:Content ContentPlaceHolderID="MainContent" runat="server">
    <%= Html.ActionLink("link 1", "Bar") %>

    <a href="<%= ViewData["Link2"]%>">link 2</a>
</asp:Content>

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about mvc