New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue U4-5853 - Allows for unit test-able UmbracoHelper and Controllers #632

wants to merge 8 commits into
base: 7.3.0


None yet
2 participants

Shazwazza commented Feb 4, 2015

This PR does quite a few things to try to decouple a lot of classes and make things more unit testable. Until we have the 'new cache' ready we can't publicize a few things that would make this process even easier, but until then this should still make things like testing Controllers possible and even relatively easy.

This splits the UmbracoHelper into it's constituent parts:

  • ITagQuery (new)
  • ITypedPublishedContentQuery (new)
  • IDynamicPublishedContentQuery (new)
  • IUmbracoComponentRenderer (new)
  • MembershipHelper
  • UrlProvider
  • IDataTypeService
  • ICultureDictionary

This also makes non-interface objects like MembershipHelper and WebSecurity to be mock-able by having virtual methods (where it makes sense) and to allow specifying correct ctor dependencies which can be mocked.

I've updated the unit tests: MockTests and SurfaceControllerTests to show how to mock the main contexts and also that you can unit test a controller using public APIs.

As noted above, there are some things that will not be testable just yet which are on the UmbracoContext until the 'new cache' is ready.

Shazwazza added some commits Feb 4, 2015

Starts breaking apart UmbracoHelper, creates new interfaces: IDynamic…
…PublishedContentQuery, ITypedPublishedContentQuery, ITagQuery, IUmbracoComponentRenderer and new ctor overloads to create a testable UmbracoHelper. There's still a little bit more to go mostly relating to members
Adds a couple of unit tests to show mocking the umbraco helper works …
…- fixes up some issues along the way

(cherry picked from commit 3fd455683fa8e313685d34c811a7440971b7b6be)
Updates HtmlStringUtilities to not have any dependencies ( the settin…
…g check is obsolete anyways ), Updates SurfaceController to lazy instantiate MembershipHelper so no YSODs are thrown if instantiated without the required nested dependencies (lazy) and makes MembershipHelper virtual so it can be overridden if required. Updates PluginController so no YSODs are thrown if instantiated without the required nested dependencies (lazy) and made all properties virtual that might want to be overridden, especially for tests. Updates MembershipHelper to allow passing in a membership/role provider so they can be mocked. Updates WebSecurity to have virtual methods on public methods so they can be mocked. Updates UmbracoContext EnsureContext methods - we only keep the legacy obsolete ones and a single one that specifies all dependencies. Updates UmbracoHelper so no YSODs are thrown if instantiated without the required nested dependencies (lazy). Updates SurfaceControllerTests to show that you can test a SurfaceController including looking up content with public APIs. Updates MockTests to show how to mock the main context objects with public APIs.
@@ -86,11 +86,13 @@ public HttpResponseMessage GetMacroResultAsHtmlForEditor(string macroAlias, int
UmbracoContext.HttpContext.Items["pageElements"] = legacyPage.Elements;
UmbracoContext.HttpContext.Items[global::Umbraco.Core.Constants.Conventions.Url.AltTemplate] = null;
var renderer = new UmbracoComponentRenderer(UmbracoContext);

This comment has been minimized.


nunofilipecosta Feb 4, 2015

Shouldn't the UmbracoComponentRenderer be passed / injected into this class ?

This comment has been minimized.


Shazwazza Feb 5, 2015


If this controller was in a user project than I would say yes! but we can't get ahead of ourselves here, internally Umbraco doesn't use IoC so it can't be 'injected' unless we create a specific controller factory which isn't in the scope of this PR. In Umbraco v8 we will have IoC built in. For now, this PR is about breaking apart UmbracoHelper and allowing better unit-testability for end-users. I'll be pulling this PR in regardless but just wanted to allow feedback on it from the community.


This comment has been minimized.


Shazwazza commented Feb 18, 2015

manually merged

@Shazwazza Shazwazza closed this Feb 18, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment