问题
I am trying to follow Jorge Lusar's suggestion on unit testing Umbraco. I could not get GetRoutingContext method to work because the Umbraco.Web.Routing.UrlProvider constructor is getting a null reference exception (I had downloaded an umbraco 7.0.4 installation previously and compiled the Umbraco.Tests.dll).
As I was getting out of options, I decided to download a fresh copy of Umbraco, compile and run a test that would execute the UrlProvider constructor. To my surprise, I got the null reference exception in there too so apparently, this bug has nothing to do with my solution but Umbraco's instead.
The images speak for themselves. Can anyone plase help with this? Is this really a bug or there's something I can do here?
回答1:
The solution to the problem was to copy the config settings (the ones in the UnitTests project of the Umbraco solution) to my test project.
Umbraco is dependendant on config files. Not ideal for unit tests but it worked.
Here it is explained how to stub Umbraco dependencies.
回答2:
Checking over a web.config for a current v7 site I've been working on, the umbracoConfiguration/settings section is of type Umbraco.Core.Configuration.UmbracoSettings.UmbracoSettingsSection
.
Another thing you're doing wrong is using as IUmbracoSettingsSection
. As means if the cast fails you return a null object, rather than an exception telling you the cast failed - it fails silently. It is better to do:
var umbracoSettings = (IUmbracoSettingsSection)ConfigurationManager.GetSection("umbracoConfiguration/settings");
As mentioned, I think your base type is wrong, and you should actually use:
var umbracoSettings = (Umbraco.Core.Configuration.UmbracoSettings.UmbracoSettingsSection)ConfigurationManager.GetSection("umbracoConfiguration/settings");
This should cast the section into the correct type for you.
来源:https://stackoverflow.com/questions/22660255/umbraco-unit-tests-failing