Adds unit test to show how to configure a test to mock the CurrentPage of a SurfaceController with public APIs
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.CodeDom;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using System.Web.Security;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
@@ -35,7 +37,7 @@ namespace Umbraco.Tests.Mvc
|
||||
var umbCtx = UmbracoContext.EnsureContext(
|
||||
new Mock<HttpContextBase>().Object,
|
||||
appCtx,
|
||||
new Mock<WebSecurity>(null, null).Object,
|
||||
new Mock<WebSecurity>(null, null).Object,
|
||||
Mock.Of<IUmbracoSettingsSection>(),
|
||||
Enumerable.Empty<IUrlProvider>(),
|
||||
true);
|
||||
@@ -74,7 +76,7 @@ namespace Umbraco.Tests.Mvc
|
||||
MockHelper.GetMockedServiceContext(),
|
||||
CacheHelper.CreateDisabledCacheHelper(),
|
||||
new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>()));
|
||||
|
||||
|
||||
var umbCtx = UmbracoContext.EnsureContext(
|
||||
new Mock<HttpContextBase>().Object,
|
||||
appCtx,
|
||||
@@ -104,7 +106,7 @@ namespace Umbraco.Tests.Mvc
|
||||
var helper = new UmbracoHelper(
|
||||
umbCtx,
|
||||
Mock.Of<IPublishedContent>(),
|
||||
Mock.Of<ITypedPublishedContentQuery>(query => query.TypedContent(It.IsAny<int>()) ==
|
||||
Mock.Of<ITypedPublishedContentQuery>(query => query.TypedContent(It.IsAny<int>()) ==
|
||||
//return mock of IPublishedContent for any call to GetById
|
||||
Mock.Of<IPublishedContent>(content => content.Id == 2)),
|
||||
Mock.Of<IDynamicPublishedContentQuery>(),
|
||||
@@ -114,12 +116,54 @@ namespace Umbraco.Tests.Mvc
|
||||
Mock.Of<ICultureDictionary>(),
|
||||
Mock.Of<IUmbracoComponentRenderer>(),
|
||||
new MembershipHelper(umbCtx, Mock.Of<MembershipProvider>(), Mock.Of<RoleProvider>()));
|
||||
|
||||
|
||||
var ctrl = new TestSurfaceController(umbCtx, helper);
|
||||
var result = ctrl.GetContent(2) as PublishedContentResult;
|
||||
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(2, result.Content.Id);
|
||||
Assert.AreEqual(2, result.Content.Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Mock_Current_Page()
|
||||
{
|
||||
var appCtx = new ApplicationContext(CacheHelper.CreateDisabledCacheHelper());
|
||||
|
||||
var webRoutingSettings = Mock.Of<IWebRoutingSection>(section => section.UrlProviderMode == "AutoLegacy");
|
||||
|
||||
var umbCtx = UmbracoContext.EnsureContext(
|
||||
new Mock<HttpContextBase>().Object,
|
||||
appCtx,
|
||||
new Mock<WebSecurity>(null, null).Object,
|
||||
Mock.Of<IUmbracoSettingsSection>(section => section.WebRouting == webRoutingSettings),
|
||||
Enumerable.Empty<IUrlProvider>(),
|
||||
true);
|
||||
|
||||
var content = Mock.Of<IPublishedContent>(publishedContent => publishedContent.Id == 12345);
|
||||
|
||||
var contextBase = umbCtx.HttpContext;
|
||||
var pcr = new PublishedContentRequest(new Uri("http://localhost/test"),
|
||||
umbCtx.RoutingContext,
|
||||
webRoutingSettings,
|
||||
s => Enumerable.Empty<string>())
|
||||
{
|
||||
PublishedContent = content
|
||||
};
|
||||
|
||||
var routeDefinition = new RouteDefinition
|
||||
{
|
||||
PublishedContentRequest = pcr
|
||||
};
|
||||
|
||||
var routeData = new RouteData();
|
||||
routeData.DataTokens.Add("umbraco-route-def", routeDefinition);
|
||||
|
||||
var ctrl = new TestSurfaceController(umbCtx, new UmbracoHelper());
|
||||
ctrl.ControllerContext = new ControllerContext(contextBase, routeData, ctrl);
|
||||
|
||||
var result = ctrl.GetContentFromCurrentPage() as PublishedContentResult;
|
||||
|
||||
Assert.AreEqual(12345, result.Content.Id);
|
||||
}
|
||||
|
||||
public class TestSurfaceController : SurfaceController
|
||||
@@ -156,6 +200,13 @@ namespace Umbraco.Tests.Mvc
|
||||
|
||||
return new PublishedContentResult(content);
|
||||
}
|
||||
|
||||
public ActionResult GetContentFromCurrentPage()
|
||||
{
|
||||
var content = CurrentPage;
|
||||
|
||||
return new PublishedContentResult(content);
|
||||
}
|
||||
}
|
||||
|
||||
public class PublishedContentResult : ActionResult
|
||||
|
||||
Reference in New Issue
Block a user