Still annoyed with unit testing Web app components

I'm sure I remember blogging about this a year ago, but I'm still annoyed with trying to unit test Web components. It's still hard to test something specific to such an application (like when you call HttpContext.Current.Server.MapPath("~/somefolder") for example).

I know the ASP.NET runtime can be hosted outside of IIS, obviously, but has anyone come up with a really good base class to derive from for Nunit tests?

Look at me, I'm worrying about this on a Saturday evening. I sure know how to party!

2 Comments

  • VSTS Whidbey comes with a unit test framework that actually hosts unit tests within the context of a web-server -- which allows you to get around the above problem.



    You could use System.Web.Hosting to create your own unit-test container -- although this wouldn't be perfect. You could fix up things like the Server.MapPath() calls above, but would have trouble simulating things like ACL permissions correctly. It would be better to build your own unittest.axd handler that you could register within the context of a real web-app, and then use it to kick off the NUnit tests. That would probably give you better fidelity to the real runtime host environment.



    Hope this helps,



    Scott

  • Any one has found better solution?

    I have initialised HttpContext like this.

    Thread.GetDomain().SetData(".appPath", @"c:\inetpub\wwwroot\Dummy");
    Thread.GetDomain().SetData(".appVPath", "/Dummy");
    Thread.GetDomain().SetData(".hostingInstallDir", HttpRuntime.AspInstallDirectory);
    TextWriter tw = new StringWriter();
    HttpWorkerRequest wr = new SimpleWorkerRequest("default.aspx", "", tw);
    HttpContext.Current = new HttpContext(wr);

    This will initialise HttpContext but I'm getting ArgumentNullException. Refer error stack.

    at System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[] str)
    at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
    at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String path)
    at System.Web.InternalSecurityPermissions.PathDiscovery(String path)
    at System.Web.HttpRequest.MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, Boolean allowCrossAppMapping)
    at System.Web.HttpRequest.MapPath(VirtualPath virtualPath)
    at System.Web.HttpRequest.MapPath(String virtualPath)
    at System.Web.HttpServerUtility.MapPath(String path)
    at TMobile.Web.AppSupport.Configuration.Configuration.get_WebsiteFeatureAvailabilityConfigFileUrl() in C:\StarTeam Work\T-Mobile.com Business Objects\PR_02_09_Source\Framework\TMobile.Web.AppSupport.Configuration\Configuration.cs:line 22
    at TMobile.Web.AppSupport.Configuration.WebsiteFeatureAvailability.IsAvailable(String websiteFeatureName) in C:\StarTeam Work\T-Mobile.com Business Objects\PR_02_09_Source\Framework\TMobile.Web.AppSupport.Configuration\WebsiteFeatureAvailability.cs:line 54
    at TMobile.Web.SqlDatabaseLog.SqlDatabaseLogger.IsLoggingEnabled(LogType logType)
    at TMobile.Web.Log.TraceMethodEnter() in C:\StarTeam Work\T-Mobile.com Business Objects\PR_02_09_Source\Framework\TMobile.Web\Log.cs:line 134
    at TMobile.Web.BusinessObjects.CardLookup.CardResult(String cardNumber) in C:\StarTeam Work\T-Mobile.com Business Objects\PR_02_09_Source\Components\TMobile.Web.BusinessObjects\CardLookup.cs:line 79
    at TMobile.Web.BusinessObjects.Test.CardLookupTest.CardResultTest() in D:\T-Mobile\NUnit\TMobile.Web.BusinessObjects.Test\CardLookupTest.cs:line 29

Comments have been disabled for this content.