Isolate your code from ASP.NET with Moles Isolation Framework

In the following example I will show how easy it is to isolate your client code from ASP.NET code, using the Moles Isolation Framework, in order to test that your code performs as intended.

The example should not be seen as an encouragement to use bad design. On the contrary, I urge you to use Moles to get that ugly, old legacy code you’ve got, and put it under test such that you will have the freedom to rip it apart and improve it.


  1. Download and install Moles Isolation Framework for .NET
  2. Open your Solution
  3. Create a test project by doing File > Add > New project > Test > Test Project
  4. Add the following references
    1. Microsoft.Moles.Framework
    2. System.Web
  5. On the test project: choose Add > New Item…
  6. Choose the Moles template “Moles and Stubs for Testing”
  7. Name it “System.Web.moles”

add new item moles

Now Moles will generate an assembly with mocks and stubs of the target assembly (System.Web) and add it to the test project. Your references should look like this:


Class under test

Now you are ready to start writing tests. First we take a look at our sample application. It is a simple ASPX-page which calls Server.MapPath() in the Page_Load method:


public partial class ServerUsageExamplePage : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {


In our unit test we want to be able to replace the call to Server.MapPath() such that

  1. We won’t get an NullReferenceException
  2. We can control what is returned

The following test method will fake the call to Server.MapPath() and assert that it was actually called by the method under test:

public void MapPath_WhenCalledWithProperContext_ShouldInvokeServerMethod() {
    // Arrange
    var mapPathWasCalled = false;
    MHttpContext.CurrentGet = () => new MHttpContext {
        ServerGet = () => new MHttpServerUtility {
            MapPathString = path => {
                mapPathWasCalled = true;
                return string.Empty;

    // Act
    var page= new ServerUsageExamplePage();
    page.Page_Load(this, EventArgs.Empty);

    // Assert

Under the covers

To accomplish this we need to understand what is going on. “Server” is an instance property on the System.Web.UI.Page class which eventually will invoke the HttpContext.Current.Server property. Thus, to fake the method call we need to fake several things:

  1. Static property HttpContext.Current
  2. Instance property Server on HttpContext
  3. Instance method MapPath on HttpServerUtility

Access modifier

Finally, to be able to execute the method under test (Page_Load), we need to change its accessibility from protected to public.


I've shown how easy it is to get started covering your ASP.NET codebehinds with unit tests utilizing Moles Isolation Framework. Please leave feedback and any questions you might have. Good luck testing!

Comments (6) -

  • alausted

    2014-02-09 00:42:02 | Reply

    Thanks for the interesting blog post. I'm new to testing frameworks so I'm still learning. Recently I inherited a brownfield app with lots of logic in the Page_Load and was looking for ways to unit test the Page_Load so that I can rip the code apart with confidence. Would Moles be a good candidate for that? Thanks!

    • Rickard

      2014-02-09 00:42:22 | Reply

      Sure, Moles can definitively help you here. Use Moles to write your unit tests against the Page_Load and once its covered with tests you can start breaking dependencies. When that work is complete, you won't need Moles anymore.

  • Giaho

    2014-02-09 00:42:54 | Reply

    Dear Rickardn,
    Thanks for this interesting article. I'm working on Microsoft Commerce Server 2009, which is another product of Microsoft, and facing with unit testing problem. It seems that we couldn't get the commerce context as expected. Would Pex & Moles works fine for that?

  • Rickard Magnusson

    2014-03-08 01:45:27 | Reply

    Ah... The other day I was looking for a solution to test an applications MapPath and Request object etc,. Guess this is the answer to that problem.
    Great article Smile

    Greetings from Sweden!

    • Rickard

      2014-03-08 13:33:24 | Reply


  • Rickard Magnusson

    2014-03-08 17:40:41 | Reply

    As a webdeveloper I concider this as a must have package... Added It to my collection of really useful extension Smile

    Thanks Rickard

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.