Learn More





DNN Community Blog

The Community Blog is a personal opinion of community members and by no means the official standpoint of DNN Corp or DNN Platform. This is a place to express personal thoughts about DNNPlatform, the community and its ecosystem. Do you have useful information that you would like to share with the DNN Community in a featured article or blog? If so, please contact .

The use of the Community Blog is covered by our Community Blog Guidelines - please read before commenting or posting.

DNN Module Development: Use Microsoft Fakes to test module code using DNN core API

When developing modules for the DNN CMS platform it is common to use the DNN (core) API to get access to entities like users, roles, settings, files, permissions, etc. Especially for complex modules that contain business logic, it is often desired to write some unit tests to ensure that the code is working as expected. A common problem when writing unit tests for module code using the DNN (core) API is: There is no running "DNN environment" which is required by some DNN core functionality. For example, some of the DNN core is dependent on an existing HTTP context by referencing System.Web.HttpContext.Current which is null when running the test cases. There are workarounds to mock the current HTTP context, but it is getting more complex if the context's session, user or cache also needs to be mocked and the HTTP context is just an example of what is needed to get a faked DNN environment.

In this blog post, I will show a solution for this problem using Microsoft Fakes. Microsoft Fakes is included in the Premium and Ultimate editions of Visual Studio 2012 (Update 2) and Visual Studio 2013. Basically by using Microsoft Fakes it is possible to create so called "shims" that can be used to solve the problem described above. Shims are described as follows:

A shim modifies the compiled code of your application at run time so that instead of making a specified method call, it runs the shim code that your test provides. Shims can be used to replace calls to assemblies that you cannot modify, such .NET assemblies.

To exemplify the problem imagine the following code that is used somewhere in the DNN module to determine the current portal's name:

public static string GetCurrentPortalName()
  return PortalSettings.Current.PortalName;

A simple unit test that ensures the method returns a portal name could look like this:

public void TestGetCurrentPortalName()
  var portalName = MyModuleClass.GetCurrentPortalName();

When running the test a NullReferenceException is thrown, because PortalSettings.Current is null. The reason is obvious: There is no DNN environment and therefore no current PortalSettings instance.

This is where Microsoft Fakes' shims come into play: With a shim it can be defined what PortalSettings.Current should be returned when running the test case. Before such a shim can be created the DotNetNuke.dll needs to be faked. This takes the following steps (read this MSDN article for more details):

  1. Add reference to DotNetNuke.dll in the test project
  2. Right click on DotNetNuke.dll under "References" and select "Add Fakes Assembly"
  3. Optionally change the (automatically) created Fakes/DotNetNuke.fakes as follows:
    <Fakes xmlns="" Diagnostic="true">
      <Assembly Name="DotNetNuke" Version="" />
        <Add Namespace="DotNetNuke.Entities.Portals"/>

    Setting Diagnostic="true" configures the Fakes generator to output all warning when creating the faked assembly. This could be useful for troubleshooting (see this Stack Overflow answer). Defining which stubs and shim should be created reduces the size and the complexity of the faked assembly.
  4. Add references to DotNetNuke.Services.Syndication.dll and PetaPoco.dll to the test project
    (the Fakes generator outputs an error that indicates they are needed to create the faked DotNetNuke.dll assembly)
  5. Manually add reference to the created fake assembly FakesAssemblies\DotNetNuke. in the test project (for any reason this reference was not automatically added)

After creating the faked assembly a shim can be defined within the test case that fakes PortalSettings.Current:

public void TestGetCurrentPortalName()
  using (ShimsContext.Create())
    // Create shims to fake PortalSettings.Current
    DotNetNuke.Entities.Portals.Fakes.ShimPortalSettings.CurrentGet = () =>
      new PortalSettings { PortalName = "My Portal Name" };

    // Test module functionality
    var portalName = MyModuleClass.GetCurrentPortalName();

You can download the Visual Studio 2013 solution containing all the source code (and the faked DotNetNuke.dll) here.

This post has been crossposted to my personal blog. See here for all my posts about DNN.


There are currently no comments, be the first to post one.

Comment Form

Only registered users may post comments.


2sic Daniel Mettler (124)
Aderson Oliveira (15)
Alec Whittington (11)
Alex Shirley (10)
Andrew Nurse (30)
Anthony Glenwright (5)
Antonio Chagoury (28)
Ash Prasad (21)
Ben Schmidt (1)
Benjamin Hermann (25)
Benoit Sarton (9)
Beth Firebaugh (12)
Bill Walker (36)
Bob Kruger (5)
Brian Dukes (2)
Brice Snow (1)
Bruce Chapman (20)
Bryan Andrews (1)
cathal connolly (55)
Charles Nurse (163)
Chris Hammond (203)
Chris Paterra (55)
Clinton Patterson (28)
Cuong Dang (21)
Daniel Bartholomew (2)
Dave Buckner (2)
David Poindexter (3)
David Rodriguez (2)
Doug Howell (11)
Erik van Ballegoij (30)
Ernst Peter Tamminga (74)
Geoff Barlow (6)
Gifford Watkins (3)
Gilles Le Pigocher (3)
Ian Robinson (7)
Israel Martinez (17)
Jan Blomquist (2)
Jan Jonas (3)
Jaspreet Bhatia (1)
Jenni Merrifield (6)
Joe Brinkman (269)
John Mitchell (1)
Jon Henning (14)
Jonathan Sheely (4)
Jordan Coopersmith (1)
Joseph Craig (2)
Kan Ma (1)
Keivan Beigi (3)
Ken Grierson (10)
Kevin Schreiner (6)
Leigh Pointer (31)
Lorraine Young (60)
Malik Khan (1)
Matthias Schlomann (15)
Mauricio Márquez (5)
Michael Doxsey (7)
Michael Tobisch (3)
Michael Washington (202)
Mike Horton (19)
Mitchel Sellers (28)
Nathan Rover (3)
Navin V Nagiah (14)
Néstor Sánchez (31)
Nik Kalyani (14)
Peter Donker (52)
Philip Beadle (135)
Philipp Becker (4)
Richard Dumas (22)
Robert J Collins (5)
Roger Selwyn (8)
Ruben Lopez (1)
Ryan Martinez (1)
Salar Golestanian (4)
Sanjay Mehrotra (9)
Scott McCulloch (1)
Scott S (11)
Scott Wilkinson (3)
Scott Willhite (97)
Sebastian Leupold (80)
Shaun Walker (237)
Shawn Mehaffie (17)
Stefan Cullmann (12)
Stefan Kamphuis (12)
Steve Fabian (31)
Timo Breumelhof (24)
Tony Henrich (3)
Torsten Weggen (2)
Vicenç Masanas (27)
Vincent Nguyen (3)
Vitaly Kozadayev (6)
Will Morgenweck (37)
Will Strohl (163)
William Severance (5)
Try Evoq
For Free
Start Free Trial
a Demo
See Evoq Live
Need More Information?