How do you get unit tests to use routes in ASP.NET MVC? -
I am writing unit tests against my ASP.NET MVC application, especially by checking a HtmlHelper extension method I am what I wrote. There is a line inside the extension method:
var innerHtml = htmlHelper.ActionLink (text, action, controller, route value, empty);
When I run it inside my unit test, the generated URL's href is empty whether the action or controller has been passed.
Here is my unit test:
var page = CreateProductDataPage (); // Return ProductData page object var htmlHelper = Http.CreateHtmlHelperWithMocks & lt; ProductDataPage & gt; (New ViewDataDictionary & lt; ProductDataPage & gt; (page), Incorrect); Var result = htmlHelper.ProductListingBreadcrumb (true, null, void);
Here's the CreateHtmlHelperWithMocks method:
Public Stable HtmlHelper & lt; T & gt; CreateHtmlHelperWithMocks & lt; T & gt; (ViewDataDictionary & lt; T & gt; ViewData, bool isLoggedIn) where T: class {var mokvidata container = new fake & lt; IViewDataContainer & gt; (); MockViewDataContainer.SetupGet (v = & gt; v.ViewData) Return (viewdata); New HtmlHelper Back & lt; T & gt; (GetViewContextMock (ViewData, isLoggedIn). Object, mockViewDataContainer.Object); }
Finally, here is the GetViewContextMock method:
public static duplicate & lt; ViewContext & gt; GetViewContextMock (ViewDataDictionary ViewData, bool isLoggedIn) {var copy = new fake & lt; ViewContext & gt; (); fake. SetupGet (V = & gt; v.HttpContext) Return (GetHttpContextMock (isLoggedIn.Object).); Fake. SetupGet (V => v.Controller). Returns (New Fake & lt; ControlBase & gt; (.) Object); Fake. SetupGet (V = & gt; v.View) Returns (New Fake & Lt; IView & gt; (Object); Fake SetupGet (v => v.ViewData) Returns (ViewData); Fake. SetupGet (v = & gt; v.TempData). Return (New TempDataDictionary ()); Fake. SetupGet (v => v.RouteData). Returns (new RootData); Fake Return;}
Update: Understand what pain is $ $ if anyone tries to do this ...
< Public static HtmlHelper & lt; T & gt; CreateHtmlHelperWithMocks & lt; T & gt; (ViewDataDictionary & lt; T & gt; ViewData, bool isLoggedIn) Where T: class {var mokvidadata container = new fake & lt; IViewDataContainer & gt; (); MockViewDataContainer.SetupGet (v => v.ViewData). Return (ViewData); // These next two lines are important: var MarginalNew = New Way Convolon (); MvcApplication.RegisterRoutes (routeCollection); New HtmlHelper Back (GetViewContextMock (ViewData, isLoggedIn). Object, mockViewDataContainer.Object, routeCollection); }
Then I had to make sure that the result of the HTTP contact mock back to the Applicationpath property and response to the ApplyAppPathModifier method it was done .
Public Stable Mocks & lt; HttpContextBase & gt; GetHttpContextMock (bool isLoggedIn) {var reference = new fake & lt; HttpContextBase & gt; (); Var Requests = New Imitation & lt; HttpRequestBase & gt; (); Var response = new mask & lt; Htppspacebase & gt; (); Var session = new fake & lt; HTPSeanStatbase & gt; (); Var server = new fake & lt; HttpServerUtilityBase & gt; (); Var Principal = Authentication and Authorization Getprinirmock (Inlongidine); // These next two lines are required for routing to generate a valid URL, apparently: request.SetupGet (r = & gt; r.ApplicationPath) .Returns ("/"); Feedback. Setup (R => RAppPluppathodifier (this. ISNAi; string & gt; ()) returns ((string r) =>); Context.SetupGet (c => c.Request). Return (request.Object); Reference.setupGet (c = & gt; c.response). Response.Object; Context.SetupGet (c => c. Session). Return (session.Object); Context.SetupGet (c = & gt; c.Server). Return (server.Object); Context.SetupGet (c = & gt; c.user). Return (Principal. Object); Return reference; }
Comments
Post a Comment