Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC Mock HttpContextBase which is used in a helper
    primarykey
    data
    text
    <p>I am using a helper in my controllers and in my views that I have found somewhere on the internet. The helper is called like this in my controller "Url.SiteRoot();" How can I get my controller to not throw an Exception whenever the helper is called? I am using MVCContrib and moq for my unit tests.</p> <p>I am thinking of implementing some kind of a check in the helper but it feel like the MVCContrib framework or the moq should be able to handle this so that I don't need to add Exception code in my helpers just to be able to pass the unit tests.</p> <p>You can see the Helper code here:-</p> <pre><code>namespace System.Web.Mvc { public static class UrlHelpers { public static string SiteRoot(HttpContextBase context) { return SiteRoot(context, true); } public static string SiteRoot(HttpContextBase context, bool usePort) { var Port = context.Request.ServerVariables["SERVER_PORT"]; if (usePort) { if (Port == null || Port == "80" || Port == "443") Port = ""; else Port = ":" + Port; } var Protocol = context.Request.ServerVariables["SERVER_PORT_SECURE"]; if (Protocol == null || Protocol == "0") Protocol = "http://"; else Protocol = "https://"; var appPath = context.Request.ApplicationPath; if (appPath == "/") appPath = ""; var sOut = Protocol + context.Request.ServerVariables["SERVER_NAME"] + Port + appPath; return sOut; } public static string SiteRoot(this UrlHelper url) { return SiteRoot(url.RequestContext.HttpContext); } public static string SiteRoot(this ViewPage pg) { return SiteRoot(pg.ViewContext.HttpContext); } public static string SiteRoot(this ViewUserControl pg) { var vpage = pg.Page as ViewPage; return SiteRoot(vpage.ViewContext.HttpContext); } public static string SiteRoot(this ViewMasterPage pg) { return SiteRoot(pg.ViewContext.HttpContext); } public static string GetReturnUrl(HttpContextBase context) { var returnUrl = ""; if (context.Request.QueryString["ReturnUrl"] != null) { returnUrl = context.Request.QueryString["ReturnUrl"]; } return returnUrl; } public static string GetReturnUrl(this UrlHelper helper) { return GetReturnUrl(helper.RequestContext.HttpContext); } public static string GetReturnUrl(this ViewPage pg) { return GetReturnUrl(pg.ViewContext.HttpContext); } public static string GetReturnUrl(this ViewMasterPage pg) { return GetReturnUrl(pg.Page as ViewPage); } public static string GetReturnUrl(this ViewUserControl pg) { return GetReturnUrl(pg.Page as ViewPage); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload