Skip to content
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

UmbracoApiController Testable #6764

Merged
merged 4 commits into from
Oct 24, 2019

Conversation

Adolfi
Copy link
Contributor

@Adolfi Adolfi commented Oct 18, 2019

From discussion on #6578 (comment) it turned out that the UmbracoApiControllerBase used the ServiceLocator to resolve the UmbracoMapper (Current.Mapper) https://github.com/umbraco/Umbraco-CMS/blob/v8/dev/src/Umbraco.Web/WebApi/UmbracoApiControllerBase.cs#L62, which made it impossible to mock/fake (unless you mock the Current.Mapper, which you shouldn't).

This PR injects the UmbracoMapper in the constructor instead and adds a obsolete attribute on the old constructor, which is kept for backward compatibility.

One tests has been added to verify that it is possible to mock the new constructor but also one that verifies that the old constructor can still resolve the Current.Mapper dependency, again for backward compatibility.

@Shazwazza was this what you had in mind?
Cheers.

…n the UmbracoApiControllerBase constructor.

* Added Obsolete marker to old UmbracoApiController constructor injecting Current.Mapper for backward compability, use new full construcor injection instead.
…ted UmbracoMapper

* Added test for mocking UmbracoApiController dependencies with ServiceLocator resolved UmbracoMapper for backward compability.
@Adolfi Adolfi changed the title V8/dev umbracoapicontroller testable UmbracoApiController Testable Oct 18, 2019
Copy link
Contributor

@Shazwazza Shazwazza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Adolfi great stuff, just a couple small tweaks

)
{ }

/// <summary>
/// Initializes a new instance of the <see cref="UmbracoApiControllerBase"/> class with all its dependencies.
/// </summary>
protected UmbracoApiControllerBase(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper)
protected UmbracoApiControllerBase(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper, UmbracoMapper umbracoMapper)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately you're going to have to obsolete this ctor as well and add another overload, just like you did with the UmbracoApiController

}

[Test]
public void Can_Mock_UmbracoApiController_Dependencies_With_ServiceLocator_UmbracoMapper()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prob don't need this test

* Added back the old construcor in UmbracoApiControllerBase and added a obsolete attribute.
@Adolfi
Copy link
Contributor Author

Adolfi commented Oct 23, 2019

Hi @Shazwazza I updated this PR to your feedback.

@emmaburstow
Copy link
Contributor

Hey @Adolfi

Thanks for the work here, and for the update too! We'll take a look and shout if we have any further suggestions or questions.

Emma

@Shazwazza
Copy link
Contributor

@Adolfi great stuff! i'll merge in , thank you 🎉

@Shazwazza Shazwazza merged commit 5121df5 into umbraco:v8/dev Oct 24, 2019
@Adolfi
Copy link
Contributor Author

Adolfi commented Oct 25, 2019

Thanks @Shazwazza! 👍🏻 Any chance you could squeeze in umbraco/UmbracoDocs#1987 also? I don’t mean to stress it but since the current documentation promotes bad practice it would be nice to get this one out as soon as possible. Cheers!!

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

Successfully merging this pull request may close these issues.

3 participants