Skip to content

Instantly share code, notes, and snippets.

@nathanwoulfe
Created April 12, 2021 23:57
Show Gist options
  • Save nathanwoulfe/84f94e6ac890c4c57c7594214a700a4c to your computer and use it in GitHub Desktop.
Save nathanwoulfe/84f94e6ac890c4c57c7594214a700a4c to your computer and use it in GitHub Desktop.
Mock IUmbracoContextFactory
private readonly IUmbracoContextFactory _context;
private readonly string _pluginPath;
public LicensingService(ILogger logger, IUmbracoContextFactory context) : base(logger)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_pluginPath = $"{IOHelper.ResolveUrl(SystemDirectories.AppPlugins).TrimEnd('/')}/plumber/";
}
public Tuple<string, string> GetLicenseAndKey() {
using (var cref = _context.EnsureUmbracoContext())
{
// under test, with a mocked _context, HttpContext and Server are null (which is expected). I don't really want to mock all the way down,
// because the context factory is a pretty large, complex object
var keyPath = cref.UmbracoContext.HttpContext.Server.MapPath($"{_pluginPath}plumber.key");
// instead, I think this would work fine, and remove the need for the factory entirely
// HttpContextManager is mine, and can be mocked
var licPath = HttpContextManager.Current.Server.MapPath($"{_pluginPath}plumber.lic");
// other stuff happens here which is irrelevant to the mocking issue...
}
}
@nathanwoulfe
Copy link
Author

Thanks Dennis, really appreciate your time. Might have to start nagging HQ to do make more ctors public

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