Created
February 11, 2018 22:35
-
-
Save aDotNetDeveloper/ce149570450f0ae8a2a580838c27bc72 to your computer and use it in GitHub Desktop.
Umbraco Test Container for mocking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using FluentAssertions; | |
using Inferno.Services.MediaCMS.Controllers; | |
using NSubstitute; | |
using Umbraco.Core; | |
using Umbraco.Core.Models; | |
using Umbraco.Core.Models.EntityBase; | |
using Xunit; | |
namespace Inferno.Services.MediaCMS.Tests.Controllers | |
{ | |
public class APIControllerTests : UmbracoTestContainer | |
{ | |
public APIControllerTests(UmbracoFixture fixture) : base(fixture) | |
{ | |
} | |
[Trait("Trait", "UnitTest")] | |
[Fact] | |
public void Get_HealthCheck_Returns500() | |
{ | |
// Arrange | |
// Setup Actual Controller | |
var controller = new APIController(this.UmbracoContext); | |
SetController(controller); | |
// Act | |
// pass nothing for IPublishedContent node - which is as invalid state | |
var result = controller.HealthCheck(null); | |
// Assert | |
result.Data.ToString().Should().Be(bool.FalseString); | |
controller.HttpContext.Response.StatusCode.Should().Be(500); | |
// test that EntityService was not called as IPublishedContent node is null | |
ApplicationContext.Current.Services.EntityService.DidNotReceive().Get(0); | |
} | |
[Trait("Trait", "UnitTest")] | |
[Fact] | |
public void Get_HealthCheck_Returns503() | |
{ | |
// Arrange | |
// Setup Actual Controller | |
var controller = new APIController(this.UmbracoContext); | |
SetController(controller); | |
var node = Substitute.For<IPublishedContent>(); | |
node.Id.Returns(1); | |
// Setup Expectations | |
var umbracoEntity = Substitute.For<IUmbracoEntity>(); | |
// force invalid umbracoEntity | |
umbracoEntity.HasIdentity.Returns(false); | |
umbracoEntity.Key.Returns(System.Guid.Empty); | |
ApplicationContext.Current.Services.EntityService.Get(1).Returns(umbracoEntity); | |
// Act | |
var result = controller.HealthCheck(node); | |
// Assert | |
result.Data.ToString().Should().Be(bool.FalseString); | |
controller.HttpContext.Response.StatusCode.Should().Be(503); | |
// test that EntityService was called as IPublishedContent node was valid | |
ApplicationContext.Current.Services.EntityService.Received().Get(1); | |
} | |
[Trait("Trait", "UnitTest")] | |
[Fact] | |
public void Get_HealthCheck_ReturnsTrue() | |
{ | |
// Arrange | |
// Setup Actual Controller | |
var controller = new APIController(this.UmbracoContext); | |
SetController(controller); | |
var node = Substitute.For<IPublishedContent>(); | |
node.Id.Returns(2); | |
// Setup Expectations | |
var umbracoEntity = Substitute.For<IUmbracoEntity>(); | |
// valid umbracoEntity | |
umbracoEntity.HasIdentity.Returns(true); | |
umbracoEntity.Key.Returns(System.Guid.NewGuid()); | |
ApplicationContext.Current.Services.EntityService.Get(2).Returns(umbracoEntity); | |
// Act | |
var result = controller.HealthCheck(node); | |
// Assert | |
result.Data.ToString().Should().Be(bool.TrueString); | |
// test that EntityService was called as IPublishedContent node was valid | |
ApplicationContext.Current.Services.EntityService.Received().Get(2); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using NSubstitute; | |
using Umbraco.Web; | |
using Umbraco.Core; | |
using Umbraco.Core.Persistence; | |
using Umbraco.Core.Persistence.SqlSyntax; | |
using Umbraco.Core.Services; | |
using Umbraco.Core.Configuration.UmbracoSettings; | |
using Umbraco.Core.Logging; | |
using Umbraco.Core.Profiling; | |
using Umbraco.Web.Security; | |
using System.Web; | |
using System.Web.Routing; | |
using Umbraco.Web.Routing; | |
using System.Linq; | |
using Umbraco.Core.Persistence.UnitOfWork; | |
namespace Inferno.Services.MediaCMS.Tests | |
{ | |
public sealed class UmbracoFixture : IDisposable | |
{ | |
private UmbracoContext _umbracoContext; | |
private HttpContextBase _httpContext; | |
private readonly ApplicationContext _applicationContext; | |
private readonly CacheHelper _cache; | |
private readonly ILogger _logger; | |
private readonly ISqlSyntaxProvider _sqlSyntaxprovider; | |
private readonly IUmbracoSettingsSection _settings; | |
private readonly IProfiler _profiler; | |
private readonly ProfilingLogger _profilingLogger; | |
private readonly WebSecurity _webSecurity; | |
private RequestContext _requestContext; | |
private readonly IScopeUnitOfWorkProvider _provider; | |
public UmbracoContext UmbracoContext => _umbracoContext ?? CreateUmbracoContext(); | |
public HttpContextBase HttpContext { get { return _httpContext ?? CreateHttpContext(); } set { _httpContext = value; } } | |
public RequestContext RequestContext => _requestContext ?? CreateRequestContext(); | |
private UmbracoContext CreateUmbracoContext() | |
{ | |
_umbracoContext = UmbracoContext.EnsureContext(HttpContext, _applicationContext, _webSecurity, _settings, Enumerable.Empty<IUrlProvider>(), true); | |
return _umbracoContext; | |
} | |
private HttpContextBase CreateHttpContext() | |
{ | |
_httpContext = Substitute.For<HttpContextBase>(); | |
var httpRequest = Substitute.For<HttpRequestBase>(); | |
var httpResponse = Substitute.For<HttpResponseBase>(); | |
httpRequest.Url.Returns(new Uri("http://localhost")); | |
_httpContext.Request.Returns(httpRequest); | |
_httpContext.Response.Returns(httpResponse); | |
_requestContext = Substitute.For<RequestContext>(); | |
_requestContext.HttpContext = _httpContext; | |
return _httpContext; | |
} | |
private RequestContext CreateRequestContext() | |
{ | |
if (_requestContext == null) | |
CreateHttpContext(); | |
return _requestContext; | |
} | |
public UmbracoFixture() | |
{ | |
_cache = CacheHelper.CreateDisabledCacheHelper(); | |
_logger = Substitute.For<ILogger>(); | |
_sqlSyntaxprovider = Substitute.For<ISqlSyntaxProvider>(); | |
_settings = Substitute.For<IUmbracoSettingsSection>(); | |
_profiler = Substitute.For<Umbraco.Core.Profiling.IProfiler>(); | |
_profilingLogger = new ProfilingLogger(_logger, _profiler); | |
_webSecurity = Substitute.For<WebSecurity>(null, null); | |
_provider = Substitute.For<Umbraco.Core.Persistence.UnitOfWork.IScopeUnitOfWorkProvider>(); | |
var repositoryFactory = new RepositoryFactory(_cache, _logger, _sqlSyntaxprovider, _settings); | |
// Currently not used, until Content and Media services are required (which is probably tomorrow) | |
var entityService = new EntityService(_provider, repositoryFactory, _logger, | |
Substitute.For<Umbraco.Core.Events.IEventMessagesFactory>(), Substitute.For<IContentService>(), Substitute.For<IContentTypeService>(), | |
Substitute.For<IMediaService>(), Substitute.For<IDataTypeService>(), Substitute.For<IMemberService>(), | |
Substitute.For<IMemberTypeService>(), Substitute.For<Umbraco.Core.Cache.IRuntimeCacheProvider>()); | |
// Umbraco Application Context | |
_applicationContext = ApplicationContext.EnsureContext( | |
new DatabaseContext(Substitute.For<IDatabaseFactory2>(), _logger, new SqlSyntaxProviders(new[] { _sqlSyntaxprovider })), | |
new ServiceContext(entityService: Substitute.For<IEntityService>()), | |
_cache, | |
_profilingLogger, true); | |
} | |
public void Dispose() | |
{ | |
ApplicationContext.Current.DisposeIfDisposable(); | |
_umbracoContext.DisposeIfDisposable(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NSubstitute; | |
using System.Web.Mvc; | |
using Umbraco.Web; | |
using Xunit; | |
namespace Inferno.Services.MediaCMS.Tests | |
{ | |
public abstract class UmbracoTestContainer : IClassFixture<UmbracoFixture> | |
{ | |
private UmbracoFixture _fixture; | |
public UmbracoContext UmbracoContext => _fixture.UmbracoContext; | |
public UmbracoTestContainer(UmbracoFixture fixture) | |
{ | |
_fixture = fixture; | |
} | |
protected void SetController(ControllerBase controller) | |
{ | |
// Controller Context | |
var controllerContext = Substitute.For<ControllerContext>(); | |
controllerContext.HttpContext = _fixture.HttpContext; | |
controllerContext.RequestContext = _fixture.RequestContext; | |
controllerContext.Controller = controller; | |
// set HTTP Context of Controller | |
controller.ControllerContext = controllerContext; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment