/// <summary> /// /// </summary> /// <param name="autoFixture"></param> /// <param name="mockServiceContainer">A service container with mockable services. One is created if it isn't provided. Provide to manually mock all Umbraco services</param> /// <param name="EnforceUniqueContentIds"></param> public UmbracoUnitTestEngine(Fixture autoFixture = null, MockServiceContext mockServiceContainer = null, bool EnforceUniqueContentIds = true) { _Fixture = autoFixture ?? new Fixture(); Content = new List <IPublishedContent>(); Media = new List <IPublishedContent>(); _mocks = new MockContainer(); mockServiceContext = mockServiceContainer ?? new MockServiceContext(); this.ApplicationContext = UmbracoUnitTestHelper.GetApplicationContext(serviceContext: this.ServiceContext, logger: _mocks.ResolveObject <ILogger>()); this.UmbracoContext = UmbracoUnitTestHelper.GetUmbracoContext(ApplicationContext, httpContext: _mocks.ResolveObject <HttpContextBase>() , webRoutingSettings: _mocks.ResolveObject <IWebRoutingSection>(), webSecurity: _mocks.ResolveObject <WebSecurity>(null, _mocks.ResolveObject <HttpContextBase>(), null)); _mocks.Resolve <IWebRoutingSection>().Setup(s => s.UrlProviderMode).Returns(UrlProviderMode.AutoLegacy.ToString()); //needed for currenttemplate, IPublishedContent.UrlAbsolute this.EnforceUniqueContentIds = EnforceUniqueContentIds; AffectsController(true, GiveRenderMvcControllerPublishedContextRouteData); _boot = UmbracoUnitTestHelper.GetCustomBootManager(serviceContext: ServiceContext); }
public void HelperContentTypeTest() { var mocks = new MockServiceContext(); var appCtx = UmbracoUnitTestHelper.GetApplicationContext(serviceContext: mocks.ServiceContext); var ctx = UmbracoUnitTestHelper.GetUmbracoContext(appCtx); //pass in emtpy proprty types to avoid uninitialized resolver issue. To bypass, must use CoreBootManager UmbracoUnitTestHelper.SetupServicesForPublishedContentTypeResolution(mocks, new PropertyType[] { }); var alias = "test_alias"; var contentType = UmbracoUnitTestHelper.GetPublishedContentType(alias: alias); var contentId = 2; //get a mocked IPublishedContent var contentMock = UmbracoUnitTestHelper.GetPublishedContentMock(contentType: contentType); var mockedTypedQuery = new Mock <ITypedPublishedContentQuery>(); mockedTypedQuery.Setup(s => s.TypedContent(It.IsAny <int>())).Returns(contentMock.Object); //give our dynamic query mock to the longer version of the UmbracoHelper constructor var helper = UmbracoUnitTestHelper.GetUmbracoHelper(ctx, typedQuery: mockedTypedQuery.Object); var controller = new BasicTestSurfaceController(ctx, helper); var res = controller.BasicContentTypeAction(contentId); var model = (string)res.Model; Assert.AreEqual(alias, model); }
public void HelperHasPropertyTest() { //Uses our special service context object (it mocks all services!!) var mockServiceContext = new MockServiceContext(); var appCtx = UmbracoUnitTestHelper.GetApplicationContext(serviceContext: mockServiceContext.ServiceContext); var ctx = UmbracoUnitTestHelper.GetUmbracoContext(appCtx); UmbracoUnitTestHelper.StartCoreBootManager(UmbracoUnitTestHelper.GetCustomBootManager(serviceContext: mockServiceContext.ServiceContext)); string propertyName = "testProp"; //THIS TIME we do need a property type defined.... this is more complicated... UmbracoUnitTestHelper.SetupServicesForPublishedContentTypeResolution(mockServiceContext, new[] { UmbracoUnitTestHelper.GetPropertyType(alias: propertyName) }); var contentId = 2; //get a mocked IPublishedContent var contentType = UmbracoUnitTestHelper.GetPublishedContentType(); var contentMock = UmbracoUnitTestHelper.GetPublishedContentMock(contentType: contentType); var mockedTypedQuery = new Mock <ITypedPublishedContentQuery>(); mockedTypedQuery.Setup(s => s.TypedContent(contentId)).Returns(contentMock.Object); //give our dynamic query mock to the longer version of the UmbracoHelper constructor var helper = UmbracoUnitTestHelper.GetUmbracoHelper(ctx, typedQuery: mockedTypedQuery.Object); var controller = new BasicTestSurfaceController(ctx, helper); var res = controller.BasicHasPropertyAction(contentId, propertyName); var model = (bool)res.Model; Assert.IsTrue(model); //clean up resolved so we can use this again... UmbracoUnitTestHelper.CleanupCoreBootManager(appCtx); }