Click here to Skip to main content
12,350,735 members (24,442 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » Howto » Downloads

Stats

57.4K views
652 downloads
35 bookmarked
Posted

Unit Testing a DotNetNuke Private Assembly Module

, 18 Jun 2007 CPOL
This article describes how to create unit tests that will test out code for a DotNetNuke custom module.
iFinity.DNN.Utilities.dll
DnnUnitTesting
bin
DnnUnitTesting
bin
Debug
obj
Debug
Refactor
TempPE
Properties
DnnUnitTesting.vsmdi
iFinity.DnnUtilities
bin
Debug
Release
iFinity.DNN.Utilities.csproj.vspscc
obj
Debug
Refactor
TempPE
Release
TempPE
Properties
localtestrun.testrunconfig
TestResults
Bruce_WOMBAT 2007-06-08 09_08_45
In
WOMBAT
Out
Bruce_WOMBAT 2007-06-08 09_10_02
In
WOMBAT
Out
Bruce_WOMBAT 2007-06-08 09_11_13
In
WOMBAT
Out
Bruce_WOMBAT 2007-06-08 09_15_14
In
WOMBAT
Out
Bruce_WOMBAT 2007-06-08 09_16_18
In
WOMBAT
Out
Bruce_WOMBAT 2007-06-08 09_16_52
In
WOMBAT
Out
Bruce_WOMBAT 2007-06-08 09_20_35
In
WOMBAT
Out
/*
 * 
 * iFinity Smart Business Solutions - http://www.ifinity.com.au
 * Copyright (c) 2006
 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 * documentation files (the "Software"), the rights to use, copy, modify, merge, publish, distribute, sublicense
 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
 * following conditions:

 * 1. The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 *    of the Software.  
 * 2. The software may not be claimed as the original work and may not be sold as a stand alone product

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 * 
 * Some portions of the source code in this file were adapted from internet sources with no rights restrictions.
 * Any perceived copyright violations should be directed to the above website.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Hosting;
using System.Web;
using System.IO;

namespace iFinity.DNN.Utilities.Testing
{

    public static class UnitTestHelper
    {
        /// <summary>
        /// Sets the HTTP context with a valid simulated request
        /// </summary>
        /// <param name="host">Host.</param>
        /// <param name="application">Application.</param>
        public static void SetHttpContextWithSimulatedRequest(string host, string application, string appPhysicalDir, string pageName)
        {

            string appVirtualDir = "/";
            string page = application.Replace("/", string.Empty) + pageName;
            string query = string.Empty;
            TextWriter output = null;

            SimulatedHttpRequest workerRequest = new SimulatedHttpRequest(appVirtualDir, appPhysicalDir, page, query, output, host);
            HttpContext.Current = new HttpContext(workerRequest);

        }

    }

    /// <summary>
    /// Used to simulate an HttpRequest.
    /// </summary>
    public class SimulatedHttpRequest : SimpleWorkerRequest
    {

        string _host;

        /// <summary>
        /// Creates a new <see cref="SimulatedHttpRequest"/> instance.
        /// </summary>
        /// <param name="appVirtualDir">App virtual dir.</param>
        /// <param name="appPhysicalDir">App physical dir.</param>
        /// <param name="page">Page.</param>
        /// <param name="query">Query.</param>
        /// <param name="output">Output.</param>
        /// <param name="host">Host.</param>
        public SimulatedHttpRequest(string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output, string host)
            : base(appVirtualDir, appPhysicalDir, page, query, output)
        {

            if (host == null || host.Length == 0)

                throw new ArgumentNullException("host", "Host cannot be null nor empty.");

            _host = host;

        }



        /// <summary>

        /// Gets the name of the server.

        /// </summary>

        /// <returns></returns>

        public override string GetServerName()
        {

            return _host;

        }



        /// <summary>

        /// Maps the path to a filesystem path.

        /// </summary>

        /// <param name="virtualPath">Virtual path.</param>

        /// <returns></returns>

        public override string MapPath(string virtualPath)
        {

            return Path.Combine(this.GetAppPath(), virtualPath);

        }

    }


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Bruce Chapman DNN
Product Manager DNN Corp
Australia Australia
Bruce Chapman is the Product Manager for Cloud Services at DNN. He’s been an active member of the DNN Community since 2006 as a contributor, vendor and now employee of DNN Corp.

You can read his blog at http://dnnsoftware.com/blog or follow him on Twitter @brucerchapman

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 19 Jun 2007
Article Copyright 2007 by Bruce Chapman DNN
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid