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:
[TestMethod]
public void TestGetCurrentPortalName()
{
var portalName = MyModuleClass.GetCurrentPortalName();
Assert.IsFalse(string.IsNullOrWhiteSpace(portalName));
}
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):
- Add reference to DotNetNuke.dll in the test project
- Right click on DotNetNuke.dll under "References" and select "Add Fakes Assembly"
- Optionally change the (automatically) created Fakes/DotNetNuke.fakes as follows:
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
<Assembly Name="DotNetNuke" Version="7.3.4.45" />
<StubGeneration>
<Clear/>
</StubGeneration>
<ShimGeneration>
<Clear/>
<Add Namespace="DotNetNuke.Entities.Portals"/>
</ShimGeneration>
</Fakes>
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.
- 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)
- Manually add reference to the created fake assembly FakesAssemblies\DotNetNuke.7.3.4.45.Fakes.dll 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:
[TestMethod]
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();
Assert.IsFalse(string.IsNullOrWhiteSpace(portalName));
}
}
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.