Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit Testing ASP.NET (not MVC) - How do I get past "Request is not Available in this context."?
    text
    copied!<p>I'm still trying to add in some unit tests for an ASP.NET site (not MVC). One of the methods I need to test makes use of the <code>HttpRequest Request</code> object, <code>Request.Path</code> to be exact. I'm trying to write the tests using Visual Studio 2008's built in testing framework. Whenever the test executes the method in question, I receive a <code>System.Web.HttpExecption: Request is not Available in this context</code>. I understand why it is not available (there is no running web server and not path supplied), but how can I proceed with testing the method? </p> <p>Since everyone likes to see code, here's the code in question:</p> <pre><code>protected string PageName { get { return Path.GetFileName(Request.Path).Substring(0, Path.GetFileName(Request.Path).Length - 5); } } protected Change SetupApproval(string changeDescription) { Change change = Change.GetInstance(); change.Description = changeDescription; change.DateOfChange = DateTime.Now; change.Page = PageName; return change; } </code></pre> <p>Here's the test:</p> <pre><code>[TestMethod] public void SetupApproval_SubmitChange_ValidateDescription() { var page = new DerivedFromInternalAppsBasePage_ForTestingOnly(); var messageToTest = "This is a test description"; var change = page.SetupApproval(messageToTest); Assert.IsTrue(messageToTest == change.Description); } </code></pre> <p>In addition, I've read through Microsoft's documentation here: <a href="http://msdn.microsoft.com/en-us/library/ms182526(v=vs.90).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms182526(v=vs.90).aspx</a> and tried using the <code>[HostType("ASP.NET")]</code>, <code>[UrlToTest(http://localhost:port/pathToAspxPage.aspx")]</code>, and <code>[AspNetDevelopmentServer("C:\PathToDllAssembly", "NotSureParameter")]</code> Attributes they suggest, but no luck. (As you can see, I'm not sure about what I should be using for a few of the parameters.).</p> <p>Lastly, I tried Phil Haack's TestWebServer <a href="http://haacked.com/archive/2006/12/12/Using_WebServer.WebDev_For_Unit_Tests.aspx" rel="nofollow">http://haacked.com/archive/2006/12/12/Using_WebServer.WebDev_For_Unit_Tests.aspx</a> and read through Scott Hanselman's post <a href="http://www.hanselman.com/blog/NUnitUnitTestingOfASPNETPagesBaseClassesControlsAndOtherWidgetryUsingCassiniASPNETWebMatrixVisualStudioWebDeveloper.aspx" rel="nofollow">http://www.hanselman.com/blog/NUnitUnitTestingOfASPNETPagesBaseClassesControlsAndOtherWidgetryUsingCassiniASPNETWebMatrixVisualStudioWebDeveloper.aspx</a> For Phil's server, I'm not sure what I would use for parameters in the <code>ExtractResource</code> method.</p>
 

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