Umbraco Unit Testing Problems due to null ApplicationContext / Umbraco Context
Hi all,
I am struggling to get Unit Testing working for Umbraco Scenarios that require UmbracoContext . I have ready many post on the topic but am still having issues …………….
var routingContext = this.GetRoutingContext("/", -1, umbracoSettings: settings);
Causes the following exception (full exception below):
Value cannot be null. Parameter name: applicationContext
On the following line of “BaseDatabaseFactoryTests.cs” ( from Umbraco.Tests.dll )
var ctx = new UmbracoContext(
httpContext,
ApplicationContext,
new PublishedCaches(cache, new PublishedMediaCache(ApplicationContext)),
new WebSecurity(httpContext, ApplicationContext));
using log4net;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using TeraTastic.Constants;
using TeraTastic.Models.TT_USPSXML;
using TeraTastic.TT_USPSXML_Classes;
using TeraTastic.TT_USPSXML_Classes.AddressValidateRequestNs;
using UCommerce.EntitiesV2;
namespace TeraTastic.TT_UnitTests
{
[TestFixture]
public class TTUSPSXMLTests
{
private static readonly ILog iMXML_Log = TeraTastic.TT_Classes.TT_Log4NetUtils.TT_Get_IMXML_Logger(typeof(TTUCTests));
[Test]
public void TT_USPS_020_ShippingRateRequest()
{
iMXML_Log.Info("TT_USPS_010_AddressValidationTest");
TTUSPSTestHelper tTUSPSTestHelper = new TTUSPSTestHelper();
tTUSPSTestHelper.TTUSPSTestInit();
var returnVal = tTUSPSTestHelper.TT_USPS_020_ShippingRateRequest_Helper();
}
// ========================================40
}
}
And here is the Helper Class that is used by the Test Class above
namespace TeraTastic.TT_UnitTests
{
public class TTUSPSTestHelper : BaseRoutingTest // <<<<<<<<<<< Note we inherit from BaseRoutingTest as recommended by blog referenced below
{
// As described at http://blog.aabech.no/archive/the-basics-of-unit-testing-umbraco/
private static IPublishedContent content;
private static UmbracoContext umbracoContext;
private static UmbracoHelper umbracoHelper;
// Note - do not override Constructor (ctor)
public void TTUSPSTestInit() {
content = Mock.Of<IPublishedContent>();
var settings = SettingsForTests.GenerateMockSettings();
// >>>>>>>>>>>>>>>>>>>>> FOLLOWING LINE CAUSES THE EXCEPTION
var routingContext = this.GetRoutingContext("/", -1, umbracoSettings: settings);
// <<<<<<<<<<< THIS IS THE LINE THAT CAUSES THE EXCEPTION
umbracoContext = routingContext.UmbracoContext;
umbracoHelper = new UmbracoHelper(umbracoContext, content);
}
public Money TT_USPS_020_ShippingRateRequest_Helper()
{
// TEST DETAILS REMOVED AS IRRELEVANT
}
}
}
FYI I am using:
• Umbraco version 7.5.10 (assembly: 1.0.6261.14290)
• UCommerce uCommerce 7.5.0.17040
• NUnit {3.10.1}
• NUnit3TestAdapter {3.10.0}
• Moq {4.1.1309.0919}
• A complete listing of Nuget installed packages is below (under the listing of the exception)
• I also added a reference to Umbraco.Tests.dll from a folder that also contains Umbraco.Tests.dll.config and Umbraco.Tests.pdb. I got these files from downloading source for Umbraco version 7.5.10 and rebuilding to create those files.
Any help or guidance resolving this would be greatly appreciated :-)
Thanks
Terry Clancy.
ClanceZ
Here is the full listing of the exception:
System.ArgumentNullException occurred
HResult=0x80004003
Message=Value cannot be null.
Parameter name: applicationContext
Source=umbraco
StackTrace:
at Umbraco.Web.PublishedCache.XmlPublishedCache.PublishedMediaCache..ctor(ApplicationContext applicationContext)
at Umbraco.Tests.TestHelpers.BaseDatabaseFactoryTest.GetUmbracoContext(String url, Int32 templateId, RouteData routeData, Boolean setSingleton) in C:\Clancy\ATeraTastic\Dev\UmbracoCMS01\UmbracoCMS.7.5.1\Umbraco-CMS-release-7.5.1\src\Umbraco.Tests\TestHelpers\BaseDatabaseFactoryTest.cs:line 364
at Umbraco.Tests.TestHelpers.BaseRoutingTest.GetRoutingContext(String url, Int32 templateId, RouteData routeData, Boolean setUmbracoContextCurrent, IUmbracoSettingsSection umbracoSettings) in C:\Clancy\ATeraTastic\Dev\UmbracoCMS01\UmbracoCMS.7.5.1\Umbraco-CMS-release-7.5.1\src\Umbraco.Tests\TestHelpers\BaseRoutingTest.cs:line 34
at TeraTastic.TTUnitTests.TTUSPSTestHelper..ctor() in C:\Clancy\ATeraTastic\Dev\TT15\TeraTastic\TTUnitTests\TTUSPSTestHelper.cs:line 40
at TeraTastic.TTUnitTests.TTUSPSXMLTests.TTUSPS020ShippingRateRequest() in C:\Clancy\ATeraTastic\Dev\TT15\TeraTastic\TT_UnitTests\TTUSPSXMLTests.cs:line 90
Here is a complete listing of Nuget installed packages:
As a result I have now removed my project reference to Umbraco.Tests.dll and removed that file as well as Umbraco.Tests.dll.config and Umbraco.Tests.pdb from the bin folder.
I then installed the "Our.Umbraco.Community.Tests" Nuget package (Version 7.5.10 to match my installed version of Umbraco.)
Unfortunately I am still getting the same error :-(
If you replace the TTUSPSTestInit() method in my original post with the following it works:
public void TTUSPSTestInit()
{
InitializeFixture(); // New
try // New
{
Initialize(); // New <<<<<<<<<<<<<<<<<< Note I think this was the main thing that fixed it.
}
catch (InvalidOperationException ex) // New
{
if (ex.Message.StartsWith("Resolution is frozen")) // New
throw new Exception("Resolution is frozen. This is probably because UmbracoSupport.DisposeUmbraco wasn't called before another UmbracoSupport.SetupUmbraco call."); // New
}
content = Mock.Of<IPublishedContent>();
var settings = SettingsForTests.GenerateMockSettings();
SettingsForTests.ConfigureSettings(settings); // New
var currentPage = Mock.Of<IPublishedContent>(); // New
var routingContext = this.GetRoutingContext("http://localhost", -1, umbracoSettings: settings);
umbracoContext = routingContext.UmbracoContext;
umbracoHelper = new UmbracoHelper(umbracoContext, content);
}
Umbraco Unit Testing Problems due to null ApplicationContext / Umbraco Context
Hi all,
I am struggling to get Unit Testing working for Umbraco Scenarios that require UmbracoContext . I have ready many post on the topic but am still having issues …………….
I am basically using the approach detailed at
http://blog.aabech.no/archive/the-basics-of-unit-testing-umbraco/ .
Unfortunately, the following Line:Causes the following exception (full exception below):
On the following line of “BaseDatabaseFactoryTests.cs” ( from Umbraco.Tests.dll )
. This same bug is described at https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/60476-unit-testing-creating-an-UmbracoContext-throws-an-exception#comment-205125 and the resolution suggested there was to use NUnit rather than MSTests. So after some work I made the move to NUnit but was disappointed to find that it did not actually salve the problem.
So here is the code that I am using:
And here is the Helper Class that is used by the Test Class above
FYI I am using:
• Umbraco version 7.5.10 (assembly: 1.0.6261.14290)
• UCommerce uCommerce 7.5.0.17040
• NUnit {3.10.1}
• NUnit3TestAdapter {3.10.0}
• Moq {4.1.1309.0919}
• A complete listing of Nuget installed packages is below (under the listing of the exception)
• I also added a reference to Umbraco.Tests.dll from a folder that also contains Umbraco.Tests.dll.config and Umbraco.Tests.pdb. I got these files from downloading source for Umbraco version 7.5.10 and rebuilding to create those files.
Any help or guidance resolving this would be greatly appreciated :-)
Thanks
Terry Clancy.
ClanceZ
Here is the full listing of the exception:
System.ArgumentNullException occurred HResult=0x80004003 Message=Value cannot be null. Parameter name: applicationContext Source=umbraco StackTrace: at Umbraco.Web.PublishedCache.XmlPublishedCache.PublishedMediaCache..ctor(ApplicationContext applicationContext) at Umbraco.Tests.TestHelpers.BaseDatabaseFactoryTest.GetUmbracoContext(String url, Int32 templateId, RouteData routeData, Boolean setSingleton) in C:\Clancy\ATeraTastic\Dev\UmbracoCMS01\UmbracoCMS.7.5.1\Umbraco-CMS-release-7.5.1\src\Umbraco.Tests\TestHelpers\BaseDatabaseFactoryTest.cs:line 364 at Umbraco.Tests.TestHelpers.BaseRoutingTest.GetRoutingContext(String url, Int32 templateId, RouteData routeData, Boolean setUmbracoContextCurrent, IUmbracoSettingsSection umbracoSettings) in C:\Clancy\ATeraTastic\Dev\UmbracoCMS01\UmbracoCMS.7.5.1\Umbraco-CMS-release-7.5.1\src\Umbraco.Tests\TestHelpers\BaseRoutingTest.cs:line 34 at TeraTastic.TTUnitTests.TTUSPSTestHelper..ctor() in C:\Clancy\ATeraTastic\Dev\TT15\TeraTastic\TTUnitTests\TTUSPSTestHelper.cs:line 40 at TeraTastic.TTUnitTests.TTUSPSXMLTests.TTUSPS020ShippingRateRequest() in C:\Clancy\ATeraTastic\Dev\TT15\TeraTastic\TT_UnitTests\TTUSPSXMLTests.cs:line 90
Here is a complete listing of Nuget installed packages:
PM> Get-Package
Id Versions ProjectName
-- -------- -----------
AutoMapper {3.0.0} Governor.Umbraco.FullTextSearch
ClientDependency {1.9.1} Governor.Umbraco.FullTextSearch
ClientDependency-Mvc {1.7.0.4} Governor.Umbraco.FullTextSearch
ClientDependency-Mvc5 {1.8.0.0} Governor.Umbraco.FullTextSearch
Examine {0.1.69.0} Governor.Umbraco.FullTextSearch
HtmlAgilityPack {1.4.9} Governor.Umbraco.FullTextSearch
ImageProcessor {2.4.4.0} Governor.Umbraco.FullTextSearch
ImageProcessor.Web {4.6.4.0} Governor.Umbraco.FullTextSearch
Lucene.Net {2.9.4.1} Governor.Umbraco.FullTextSearch
Lucene.Net.Contrib {2.9.4.1} Governor.Umbraco.FullTextSearch
Markdown {1.14.4} Governor.Umbraco.FullTextSearch
Microsoft.AspNet.Identity.Core {2.2.1} Governor.Umbraco.FullTextSearch
Microsoft.AspNet.Identity.Owin {2.2.1} Governor.Umbraco.FullTextSearch
Microsoft.AspNet.Mvc {5.2.3} Governor.Umbraco.FullTextSearch
Microsoft.AspNet.Mvc.FixedDispla... {5.0.0} Governor.Umbraco.FullTextSearch
Microsoft.AspNet.Razor {3.2.3} Governor.Umbraco.FullTextSearch
Microsoft.AspNet.WebApi {5.2.3} Governor.Umbraco.FullTextSearch
Microsoft.AspNet.WebApi.Client {5.2.3} Governor.Umbraco.FullTextSearch
Microsoft.AspNet.WebApi.Core {5.2.3} Governor.Umbraco.FullTextSearch
Microsoft.AspNet.WebApi.WebHost {5.2.3} Governor.Umbraco.FullTextSearch
Microsoft.AspNet.WebPages {3.2.3} Governor.Umbraco.FullTextSearch
Microsoft.Bcl {1.1.9} Governor.Umbraco.FullTextSearch
Microsoft.Bcl.Build {1.0.14} Governor.Umbraco.FullTextSearch
Microsoft.Net.Http {2.2.28} Governor.Umbraco.FullTextSearch
Microsoft.Owin {3.0.1} Governor.Umbraco.FullTextSearch
Microsoft.Owin.Host.SystemWeb {3.0.1} Governor.Umbraco.FullTextSearch
Microsoft.Owin.Security {3.0.1} Governor.Umbraco.FullTextSearch
Microsoft.Owin.Security.Cookies {3.0.1} Governor.Umbraco.FullTextSearch
Microsoft.Owin.Security.OAuth {3.0.1} Governor.Umbraco.FullTextSearch
Microsoft.Web.Infrastructure {1.0.0.0} Governor.Umbraco.FullTextSearch
MiniProfiler {2.1.0} Governor.Umbraco.FullTextSearch
MySql.Data {6.9.9} Governor.Umbraco.FullTextSearch
Newtonsoft.Json {6.0.8} Governor.Umbraco.FullTextSearch
Owin {1.0} Governor.Umbraco.FullTextSearch
semver {1.1.2} Governor.Umbraco.FullTextSearch
SharpZipLib {0.86.0} Governor.Umbraco.FullTextSearch
UmbracoCms.Core {7.5.0} Governor.Umbraco.FullTextSearch
UrlRewritingNet {2.0.7} Governor.Umbraco.FullTextSearch
xmlrpcnet {2.5.0} Governor.Umbraco.FullTextSearch
AutoMapper {3.0.0} uCommerce.RazorStore
bootstrap {3.0.0} uCommerce.RazorStore
bootstrap.sass {4.0.0-alpha3} uCommerce.RazorStore
ClientDependency {1.8.4} uCommerce.RazorStore
ClientDependency-Mvc {1.8.0.0} uCommerce.RazorStore
Examine {0.1.68.0} uCommerce.RazorStore
Gulp.js {1.0.2} uCommerce.RazorStore
HtmlAgilityPack {1.4.9} uCommerce.RazorStore
ImageProcessor {1.9.5.0} uCommerce.RazorStore
ImageProcessor.Web {3.3.1.0} uCommerce.RazorStore
jQuery {3.1.1} uCommerce.RazorStore
Lucene.Net {2.9.4.1} uCommerce.RazorStore
Markdown {1.14.4} uCommerce.RazorStore
Microsoft.AspNet.Identity.Core {2.2.1} uCommerce.RazorStore
Microsoft.AspNet.Identity.Owin {2.2.1} uCommerce.RazorStore
Microsoft.AspNet.Mvc {4.0.20710.0} uCommerce.RazorStore
Microsoft.AspNet.Mvc.FixedDispla... {1.0.1} uCommerce.RazorStore
Microsoft.AspNet.Razor {2.0.30506.0} uCommerce.RazorStore
Microsoft.AspNet.WebApi {4.0.30506.0} uCommerce.RazorStore
Microsoft.AspNet.WebApi.Client {4.0.30506.0} uCommerce.RazorStore
Microsoft.AspNet.WebApi.Core {4.0.30506.0} uCommerce.RazorStore
Microsoft.AspNet.WebApi.WebHost {4.0.30506.0} uCommerce.RazorStore
Microsoft.AspNet.WebPages {2.0.30506.0} uCommerce.RazorStore
Microsoft.Net.Http {2.0.20710.0} uCommerce.RazorStore
Microsoft.Owin {3.0.1} uCommerce.RazorStore
Microsoft.Owin.Host.SystemWeb {3.0.1} uCommerce.RazorStore
Microsoft.Owin.Security {3.0.1} uCommerce.RazorStore
Microsoft.Owin.Security.Cookies {3.0.1} uCommerce.RazorStore
Microsoft.Owin.Security.OAuth {3.0.1} uCommerce.RazorStore
Microsoft.Web.Infrastructure {1.0.0.0} uCommerce.RazorStore
MiniProfiler {2.1.0} uCommerce.RazorStore
Modernizr {2.6.2} uCommerce.RazorStore
MySql.Data {6.6.5} uCommerce.RazorStore
Newtonsoft.Json {6.0.8} uCommerce.RazorStore
Node.js {5.3.0} uCommerce.RazorStore
Npm.js {2.13.1.0} uCommerce.RazorStore
Owin {1.0} uCommerce.RazorStore
semver {1.1.2} uCommerce.RazorStore
SharpZipLib {0.86.0} uCommerce.RazorStore
UmbracoCms.Core {7.1.4} uCommerce.RazorStore
UrlRewritingNet {2.0.7} uCommerce.RazorStore
xmlrpcnet {2.5.0} uCommerce.RazorStore
AutoMapper {3.3.1} TeraTastic
bootstrap {3.0.0} TeraTastic
Castle.Core {3.3.3} TeraTastic
ClientDependency {1.9.2} TeraTastic
ClientDependency-Mvc5 {1.8.0.0} TeraTastic
Examine {0.1.81} TeraTastic
HtmlAgilityPack {1.4.9} TeraTastic
ImageProcessor {2.5.2} TeraTastic
ImageProcessor.Web {4.8.2} TeraTastic
ImageProcessor.Web.Config {2.3.0.0} TeraTastic
jQuery {1.10.2} TeraTastic
Lucene.Net {2.9.4.1} TeraTastic
Markdown {1.14.7} TeraTastic
Microsoft.AspNet.Identity.Core {2.2.1} TeraTastic
Microsoft.AspNet.Identity.Owin {2.2.1} TeraTastic
Microsoft.AspNet.Mvc {5.2.3} TeraTastic
Microsoft.AspNet.Razor {3.2.3} TeraTastic
Microsoft.AspNet.WebApi {5.2.3} TeraTastic
Microsoft.AspNet.WebApi.Client {5.2.3} TeraTastic
Microsoft.AspNet.WebApi.Core {5.2.3} TeraTastic
Microsoft.AspNet.WebApi.WebHost {5.2.3} TeraTastic
Microsoft.AspNet.WebPages {3.2.3} TeraTastic
Microsoft.CodeAnalysis.Analyzers {1.0.0} TeraTastic
Microsoft.CodeAnalysis.Common {1.0.0} TeraTastic
Microsoft.CodeAnalysis.CSharp {1.0.0} TeraTastic
Microsoft.CodeDom.Providers.DotN... {1.0.0} TeraTastic
Microsoft.IO.RecyclableMemoryStream {1.2.1} TeraTastic
Microsoft.Net.Compilers {1.0.0} TeraTastic
Microsoft.Owin {3.0.1} TeraTastic
Microsoft.Owin.Host.SystemWeb {3.0.1} TeraTastic
Microsoft.Owin.Security {3.0.1} TeraTastic
Microsoft.Owin.Security.Cookies {3.0.1} TeraTastic
Microsoft.Owin.Security.Facebook {3.0.1} TeraTastic
Microsoft.Owin.Security.OAuth {3.0.1} TeraTastic
Microsoft.Web.Infrastructure {1.0.0.0} TeraTastic
MiniProfiler {2.1.0} TeraTastic
Modernizr {2.6.2} TeraTastic
Moq {4.1.1309.0919} TeraTastic
MySql.Data {6.9.9} TeraTastic
Newtonsoft.Json {6.0.8} TeraTastic
NUnit {3.10.1} TeraTastic
NUnit3TestAdapter {3.10.0} TeraTastic
Owin {1.0} TeraTastic
semver {1.1.2} TeraTastic
SharpZipLib {0.86.0} TeraTastic
System.Collections.Immutable {1.1.36} TeraTastic
System.Reflection.Metadata {1.0.21} TeraTastic
Umbraco.ModelsBuilder {3.0.5} TeraTastic
UmbracoCms {7.5.10} TeraTastic
UmbracoCms.Core {7.5.10} TeraTastic
UmbracoIdentity {5.0.0} TeraTastic
UmbracoIdentity.Core {5.0.0} TeraTastic
UrlRewritingNet {2.0.7} TeraTastic
xmlrpcnet {2.5.0} TeraTastic
Thanks again
Terry Clancy ClanceZ
All,
An Update.... I have just read Lars-Erik's latter blog post at http://blog.aabech.no/archive/the-basics-of-unit-testing-umbraco-just-got-simpler/.
As a result I have now removed my project reference to Umbraco.Tests.dll and removed that file as well as Umbraco.Tests.dll.config and Umbraco.Tests.pdb from the bin folder.
I then installed the "Our.Umbraco.Community.Tests" Nuget package (Version 7.5.10 to match my installed version of Umbraco.)
Unfortunately I am still getting the same error :-(
Terry Clancy
Hi Terry,
I see you are using NUnit 3. If i'm correct, to use the umbraco test helpers you need to "downgrade" to NUnit 2.6. That is the version in Umbraco Core
And that works for us.
Dave
And just checked a Umbraco 7.5 project we have running with tests
Dave ,
Thanks so much. Looks like we were working in parallel. I was studying Lars-Erik's blog at http://blog.aabech.no/archive/the-basics-of-unit-testing-umbraco-just-got-simpler/ in great detail and solved the problem as follows
If you replace the TTUSPSTestInit() method in my original post with the following it works:
Note that even though I have this working I think I will thow that code away and use the "UmbracoSupport" Class and related approach as detailed by Lars-Erik's (after the heading "THE IMPLEMENTATION") in his blog at http://blog.aabech.no/archive/the-basics-of-unit-testing-umbraco-just-got-simpler/ .
Dave's approach above also looks to be viable alternative and Dave thanks again for that.
Thanks all :-)
Terry Clancy ClanceZ
Hi Terry,
In newer projects we also switched to the Umbraco Support class.
But this is an older project where I copied the code from, before the Umbraco Support thing was released.
Dave
Dave,
Thanks for the follow-up - good to know. Have a good weekend :-)
Terry Clancy
ClanceZ
is working on a reply...