-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
UmbracoApiController Testable #6764
Conversation
…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.
There was a problem hiding this 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) |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.
Hi @Shazwazza I updated this PR to your feedback. |
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 |
@Adolfi great stuff! i'll merge in , thank you 🎉 |
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!! |
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.