public void GetControllerTypeSearchesRouteDefinedNamespacesBeforeApplicationDefinedNamespaces() { // Arrange RequestContext requestContext = GetRequestContextWithNamespaces("ns3a.ns3b"); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; factory.RequestContext = requestContext; // Act Type c1Type = factory.GetControllerType("C1"); Type c2Type = factory.GetControllerType("C2"); // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assembly asm3 = Assembly.Load("MvcAssembly3"); Type verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller"); Assert.IsNotNull(verifiedC1, "Couldn't find real C1 type"); Assert.IsNotNull(verifiedC2, "Couldn't find real C2 type"); Assert.AreEqual <Type>(verifiedC1, c1Type, "Should have found C1Controller type."); Assert.AreEqual <Type>(verifiedC2, c2Type, "Should have found C2Controller type."); Assert.AreEqual <int>(4, controllerTypeCache.Count, "Cache should have 4 controller types."); }
public void RenderWithViewPageAndStartPageLookupExecutesStartPage() { // Arrange StubWebViewPage viewPage = new StubWebViewPage(); Mock <ViewContext> viewContextMock = new Mock <ViewContext>(); viewContextMock.Setup(vc => vc.HttpContext.Items).Returns(new Dictionary <object, object>()); MockBuildManager buildManager = new MockBuildManager("~/viewPath", typeof(object)); Mock <IViewPageActivator> activator = new Mock <IViewPageActivator>(MockBehavior.Strict); ControllerContext controllerContext = new ControllerContext(); activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewPage); RazorView view = new RazorView(controllerContext, "~/viewPath", null, true, new[] { "cshtml" }, activator.Object); Mock <ViewStartPage> startPage = new Mock <ViewStartPage>(); startPage.Setup(sp => sp.ExecutePageHierarchy()).Verifiable(); view.StartPageLookup = (WebPageRenderingBase page, string fileName, IEnumerable <string> extensions) => { Assert.Equal(viewPage, page); Assert.Equal("_ViewStart", fileName); Assert.Equal(new[] { "cshtml" }, extensions.ToArray()); return(startPage.Object); }; view.BuildManager = buildManager; // Act view.Render(viewContextMock.Object, new Mock <TextWriter>().Object); // Assert startPage.Verify(sp => sp.ExecutePageHierarchy(), Times.Once()); }
public void GetControllerTypeForManyAssemblies() { // Arrange DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b", "ns3a.ns3b", "ns4a.ns4b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly2") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType("C1"); Type c2Type = factory.GetControllerType("C2"); Type c3Type = factory.GetControllerType("c3"); // lower case Type c4Type = factory.GetControllerType("c4"); // lower case // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assembly asm2 = Assembly.Load("MvcAssembly2"); Type verifiedC3 = asm2.GetType("NS3a.NS3b.C3Controller"); Type verifiedC4 = asm2.GetType("NS4a.NS4b.C4Controller"); Assert.IsNotNull(verifiedC1, "Couldn't find real C1 type"); Assert.IsNotNull(verifiedC2, "Couldn't find real C2 type"); Assert.IsNotNull(verifiedC3, "Couldn't find real C3 type"); Assert.IsNotNull(verifiedC4, "Couldn't find real C4 type"); Assert.AreEqual <Type>(verifiedC1, c1Type, "Should have found C1Controller type."); Assert.AreEqual <Type>(verifiedC2, c2Type, "Should have found C2Controller type."); Assert.AreEqual <Type>(verifiedC3, c3Type, "Should have found C3Controller type."); Assert.AreEqual <Type>(verifiedC4, c4Type, "Should have found C4Controller type."); Assert.AreEqual <int>(4, controllerTypeCache.Count, "Cache should have 4 controller types."); }
public void GetControllerTypeForAssembliesWithSameTypeNamesInDifferentNamespaces() { // Arrange DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType("C1"); Type c2Type = factory.GetControllerType("C2"); // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assert.IsNotNull(verifiedC1, "Couldn't find real C1 type"); Assert.IsNotNull(verifiedC2, "Couldn't find real C2 type"); Assert.AreEqual <Type>(verifiedC1, c1Type, "Should have found C1Controller type."); Assert.AreEqual <Type>(verifiedC2, c2Type, "Should have found C2Controller type."); Assert.AreEqual <int>(4, controllerTypeCache.Count, "Cache should have 4 controller types."); }
public void GetControllerTypeForAssembliesWithSameTypeNamesInDifferentNamespaces() { // Arrange RequestContext requestContext = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData()); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType(requestContext, "C1"); Type c2Type = factory.GetControllerType(requestContext, "C2"); // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assert.NotNull(verifiedC1); Assert.NotNull(verifiedC2); Assert.Equal(verifiedC1, c1Type); Assert.Equal(verifiedC2, c2Type); Assert.Equal(4, controllerTypeCache.Count); }
public void GetControllerTypeSearchesOnlyRouteDefinedNamespacesIfRequested() { // Arrange RequestContext requestContext = GetRequestContextWithNamespaces("ns3a.ns3b"); requestContext.RouteData.DataTokens["UseNamespaceFallback"] = false; DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType(requestContext, "C1"); Type c2Type = factory.GetControllerType(requestContext, "C2"); // Assert Assembly asm3 = Assembly.Load("MvcAssembly3"); Type verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller"); Assert.NotNull(verifiedC1); Assert.Equal(verifiedC1, c1Type); Assert.Null(c2Type); }
public void GetControllerTypeSearchesRouteDefinedNamespacesBeforeApplicationDefinedNamespaces() { // Arrange RequestContext requestContext = GetRequestContextWithNamespaces("ns3a.ns3b"); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType(requestContext, "C1"); Type c2Type = factory.GetControllerType(requestContext, "C2"); // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assembly asm3 = Assembly.Load("MvcAssembly3"); Type verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller"); Assert.NotNull(verifiedC1); Assert.NotNull(verifiedC2); Assert.Equal(verifiedC1, c1Type); Assert.Equal(verifiedC2, c2Type); Assert.Equal(4, controllerTypeCache.Count); }
public void GetControllerTypeDoesNotThrowIfSameControllerMatchedMultipleNamespaces() { // both namespaces "ns3a" and "ns3a.ns3b" will match a controller type, but it's actually // the same type. in this case, we shouldn't throw. // Arrange RequestContext requestContext = GetRequestContextWithNamespaces("ns3a", "ns3a.ns3b"); requestContext.RouteData.DataTokens["UseNamespaceFallback"] = false; DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly3") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType(requestContext, "C1"); // Assert Assembly asm3 = Assembly.Load("MvcAssembly3"); Type verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller"); Assert.NotNull(verifiedC1); Assert.Equal(verifiedC1, c1Type); }
public void GetControllerTypeForManyAssemblies() { // Arrange RequestContext requestContext = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData()); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b", "ns3a.ns3b", "ns4a.ns4b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly2") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType(requestContext, "C1"); Type c2Type = factory.GetControllerType(requestContext, "C2"); Type c3Type = factory.GetControllerType(requestContext, "c3"); // lower case Type c4Type = factory.GetControllerType(requestContext, "c4"); // lower case // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assembly asm2 = Assembly.Load("MvcAssembly2"); Type verifiedC3 = asm2.GetType("NS3a.NS3b.C3Controller"); Type verifiedC4 = asm2.GetType("NS4a.NS4b.C4Controller"); Assert.NotNull(verifiedC1); Assert.NotNull(verifiedC2); Assert.NotNull(verifiedC3); Assert.NotNull(verifiedC4); Assert.Equal(verifiedC1, c1Type); Assert.Equal(verifiedC2, c2Type); Assert.Equal(verifiedC3, c3Type); Assert.Equal(verifiedC4, c4Type); Assert.Equal(4, controllerTypeCache.Count); }
public void ViewCreationDelegatesToDependencyResolverWhenActivatorIsNull() { // Arrange var viewInstance = new object(); var controllerContext = new ControllerContext(); var buildManager = new MockBuildManager("view path", typeof(object)); var dependencyResolver = new Mock <IDependencyResolver>(MockBehavior.Strict); dependencyResolver .Setup(dr => dr.GetService(typeof(object))) .Returns(viewInstance) .Verifiable(); var view = new TestableBuildManagerCompiledView( controllerContext, "view path", dependencyResolver: dependencyResolver.Object ) { BuildManager = buildManager }; // Act view.Render(new Mock <ViewContext>().Object, new Mock <TextWriter>().Object); // Assert dependencyResolver.Verify(); }
public void ViewCreationConsultsSetActivator() { // Arrange object viewInstance = new object(); Mock <IViewPageActivator> activator = new Mock <IViewPageActivator>(MockBehavior.Strict); ControllerContext controllerContext = new ControllerContext(); activator .Setup(a => a.Create(controllerContext, typeof(object))) .Returns(viewInstance) .Verifiable(); MockBuildManager buildManager = new MockBuildManager("view path", typeof(object)); BuildManagerCompiledView view = new TestableBuildManagerCompiledView( controllerContext, "view path", activator.Object ) { BuildManager = buildManager }; // Act view.Render(new Mock <ViewContext>().Object, new Mock <TextWriter>().Object); // Assert activator.Verify(); }
public void GetFilteredTypesFromAssemblies_FallThrough() { // Arrange Type[] expectedTypes = new Type[] { typeof(TypeCacheValidFoo), typeof(TypeCacheValidBar) }; string cacheName = "testCache"; MockBuildManager buildManager = new MockBuildManager(); Predicate <Type> predicate = type => type.IsDefined(typeof(TypeCacheMarkerAttribute), true); // Act List <Type> returnedTypes = TypeCacheUtil.GetFilteredTypesFromAssemblies( cacheName, predicate, buildManager ); // Assert Assert.Equal(expectedTypes, returnedTypes.ToArray()); MemoryStream cachedStream = buildManager.CachedFileStore[cacheName] as MemoryStream; Assert.NotNull(cachedStream); Assert.NotEmpty(cachedStream.ToArray()); }
public void RenderWithViewPageRendersView() { // Arrange ViewContext context = new Mock <ViewContext>().Object; MockBuildManager buildManager = new MockBuildManager("view path", typeof(object)); Mock <IViewPageActivator> activator = new Mock <IViewPageActivator>(MockBehavior.Strict); ControllerContext controllerContext = new ControllerContext(); StubViewPage viewPage = new StubViewPage(); activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewPage); WebFormView view = new WebFormView( controllerContext, "view path", null, activator.Object ); view.BuildManager = buildManager; // Act view.Render(context, null); // Assert Assert.Equal(context, viewPage.ResultViewContext); Assert.Equal(String.Empty, viewPage.MasterLocation); }
public void GetControllerTypeForAssembliesWithSameTypeNamesInSameNamespaceThrows() { // Arrange RequestContext requestContext = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData()); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly4") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act ExceptionHelper.ExpectException <InvalidOperationException>( delegate { factory.GetControllerType(requestContext, "C1"); }, @"Multiple types were found that match the controller named 'C1'. This can happen if the route that services this request does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter. The request for 'C1' has found the following matching controllers: NS1a.NS1b.C1Controller NS1a.NS1b.C1Controller"); // Assert Assert.AreEqual <int>(4, controllerTypeCache.Count, "Cache should have 4 controller types."); }
public void RenderWithViewPageAndNoStartPageLookupRendersView() { // Arrange StubWebViewPage viewPage = new StubWebViewPage(); Mock <ViewContext> viewContextMock = new Mock <ViewContext>(); viewContextMock.Setup(vc => vc.HttpContext.Items).Returns(new Dictionary <object, object>()); viewContextMock.Setup(vc => vc.HttpContext.Request.IsLocal).Returns(false); MockBuildManager buildManager = new MockBuildManager("~/viewPath", typeof(object)); Mock <IViewPageActivator> activator = new Mock <IViewPageActivator>(MockBehavior.Strict); ControllerContext controllerContext = new ControllerContext(); activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewPage); RazorView view = new RazorView(controllerContext, "~/viewPath", null, false, Enumerable.Empty <string>(), activator.Object); view.StartPageLookup = (WebPageRenderingBase p, string n, IEnumerable <string> e) => { Assert.True(false, "ViewStart page lookup should not be called"); return(null); }; view.BuildManager = buildManager; // Act view.Render(viewContextMock.Object, new Mock <TextWriter>().Object); // Assert Assert.Null(viewPage.Layout); Assert.Equal("", viewPage.OverridenLayoutPath); Assert.Same(viewContextMock.Object, viewPage.ViewContext); Assert.Equal("~/viewPath", viewPage.VirtualPath); }
public void RenderWithViewPageAndLayoutPageRendersView() { // Arrange StubWebViewPage viewPage = new StubWebViewPage(); Mock <ViewContext> viewContext = new Mock <ViewContext>(); Mock <HttpContextBase> httpContext = new Mock <HttpContextBase>(); Mock <HttpRequestBase> httpRequest = new Mock <HttpRequestBase>(); httpRequest.SetupGet(r => r.IsLocal).Returns(false); httpRequest.SetupGet(r => r.Browser.IsMobileDevice).Returns(false); httpRequest.SetupGet(r => r.Cookies).Returns(new HttpCookieCollection()); httpContext.SetupGet(c => c.Request).Returns(httpRequest.Object); httpContext.SetupGet(c => c.Response.Cookies).Returns(new HttpCookieCollection()); httpContext.SetupGet(c => c.Items).Returns(new Hashtable()); viewContext.SetupGet(v => v.HttpContext).Returns(httpContext.Object); MockBuildManager buildManager = new MockBuildManager("~/viewPath", typeof(object)); Mock <IViewPageActivator> activator = new Mock <IViewPageActivator>(MockBehavior.Strict); Mock <WebPage> layoutPage = new Mock <WebPage> { CallBase = true }; layoutPage.Setup(c => c.Execute()).Callback(() => layoutPage.Object.RenderBody()); Mock <IVirtualPathFactory> virtualPathFactory = new Mock <IVirtualPathFactory>( MockBehavior.Strict ); virtualPathFactory.Setup(f => f.Exists("~/layoutPath")).Returns(true); virtualPathFactory .Setup(f => f.CreateInstance("~/layoutPath")) .Returns(layoutPage.Object); ControllerContext controllerContext = new ControllerContext(); activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewPage); RazorView view = new RazorView( controllerContext, "~/viewPath", "~/layoutPath", false, Enumerable.Empty <string>(), activator.Object ); view.BuildManager = buildManager; view.VirtualPathFactory = virtualPathFactory.Object; view.DisplayModeProvider = DisplayModeProvider.Instance; // Act view.Render(viewContext.Object, TextWriter.Null); // Assert Assert.Equal("~/layoutPath", viewPage.Layout); Assert.Equal("~/layoutPath", viewPage.OverridenLayoutPath); Assert.Same(viewContext.Object, viewPage.ViewContext); Assert.Equal("~/viewPath", viewPage.VirtualPath); }
public void BuildManagerProperty() { // Arrange var engine = new TestableBuildManagerViewEngine(); var buildManagerMock = new MockBuildManager(expectedVirtualPath: null, compiledType: null); // Act engine.BuildManager = buildManagerMock; // Assert Assert.AreSame(engine.BuildManager, buildManagerMock); }
public void BuildManagerProperty() { // Arrange TestableWebFormViewEngine engine = new TestableWebFormViewEngine(); MockBuildManager buildManagerMock = new MockBuildManager(null, null, null); // Act engine.BuildManager = buildManagerMock; // Assert Assert.AreEqual(engine.BuildManager, buildManagerMock); }
public void FileExistsReturnsFalseWhenBuildManagerReturnsNull() { // Arrange TestableWebFormViewEngine engine = new TestableWebFormViewEngine(); MockBuildManager buildManagerMock = new MockBuildManager("some path", typeof(object), null); engine.BuildManager = buildManagerMock; // Act bool notFoundResult = engine.PublicFileExists(null, "some path"); // Assert Assert.IsFalse(notFoundResult); }
public void RegisterAllAreas() { // Arrange string[] expectedLoadedAreas = new string[] { "AreaRegistrationTest_AreaRegistration" }; AnnotatedRouteCollection routes = new AnnotatedRouteCollection(); MockBuildManager buildManager = new MockBuildManager(new Assembly[] { typeof(AreaRegistrationTest).Assembly }); // Act AreaRegistration.RegisterAllAreas(routes, buildManager, null); // Assert CollectionAssert.AreEquivalent(expectedLoadedAreas, routes._areasLoaded); }
public void FileExistsReturnsFalseWhenBuildManagerFileExistsReturnsFalse() { // Arrange var engine = new TestableBuildManagerViewEngine(); var buildManagerMock = new MockBuildManager("some path", false); engine.BuildManager = buildManagerMock; // Act bool result = engine.FileExists("some path"); // Assert Assert.IsFalse(result); }
public void FileExistsReturnsTrueForExistingPath() { // Arrange var engine = new TestableBuildManagerViewEngine(); var buildManagerMock = new MockBuildManager("some path", typeof(object)); engine.BuildManager = buildManagerMock; // Act bool result = engine.FileExists("some path"); // Assert Assert.IsTrue(result); }
public void BuildManagerProperty() { // Arrange var engine = new TestableBuildManagerViewEngine(); var buildManagerMock = new MockBuildManager(expectedVirtualPath: null, compiledType: null); // Act engine.BuildManager = buildManagerMock; // Assert Assert.Same(engine.BuildManager, buildManagerMock); }
public void RenderWithNullViewInstanceThrows() { // Arrange ViewContext context = new Mock<ViewContext>().Object; MockBuildManager buildManager = new MockBuildManager("view path", compiledType: null); TestableBuildManagerCompiledView view = new TestableBuildManagerCompiledView(new ControllerContext(), "view path"); view.BuildManager = buildManager; // Act & Assert ExceptionHelper.ExpectException<InvalidOperationException>( () => view.Render(context, new Mock<TextWriter>().Object), "The view found at 'view path' was not created." ); }
public void RegisterAllAreas() { // Arrange string[] expectedLoadedAreas = new string[] { "AreaRegistrationTest_AreaRegistration" }; AnnotatedRouteCollection routes = new AnnotatedRouteCollection(); MockBuildManager buildManager = new MockBuildManager(new Assembly[] { typeof(AreaRegistrationTest).Assembly }); // Act AreaRegistration.RegisterAllAreas(routes, buildManager, null); // Assert Assert.Equal(expectedLoadedAreas, routes._areasLoaded.ToArray()); }
public void FileExistsReturnsTrueForExistingPath() { // Arrange TestableWebFormViewEngine engine = new TestableWebFormViewEngine(); object instanceResult = new object(); MockBuildManager buildManagerMock = new MockBuildManager("some path", typeof(object), instanceResult); engine.BuildManager = buildManagerMock; // Act bool foundResult = engine.PublicFileExists(null, "some path"); // Assert Assert.IsTrue(foundResult); }
public void RenderWithUnsupportedTypeThrows() { // Arrange ViewContext context = new Mock<ViewContext>().Object; MockBuildManager buildManagerMock = new MockBuildManager("view path", typeof(object), 12345); WebFormView view = new WebFormView("view path", null); view.BuildManager = buildManagerMock; // Act & Assert ExceptionHelper.ExpectException<InvalidOperationException>( () => view.Render(context, null), "The view at 'view path' must derive from ViewPage, ViewPage<TViewData>, ViewUserControl, or ViewUserControl<TViewData>." ); }
public void FileExistsReturnsFalseWhenBuildManagerThrows404() { // Arrange TestableWebFormViewEngine engine = new TestableWebFormViewEngine(); object instanceResult = new object(); MockBuildManager buildManagerMock = new MockBuildManager(new HttpException(404, "HTTP message Not Found")); engine.BuildManager = buildManagerMock; // Act bool notFoundResult = engine.PublicFileExists(null, "some other path"); // Assert Assert.IsFalse(notFoundResult); }
public void RenderWithNullViewInstanceThrows() { // Arrange ViewContext context = new Mock<ViewContext>().Object; MockBuildManager buildManager = new MockBuildManager("view path", typeof(object), null); WebFormView view = new WebFormView("view path", null); view.BuildManager = buildManager; // Act & Assert ExceptionHelper.ExpectException<InvalidOperationException>( () => view.Render(context, null), "The view found at 'view path' could not be created." ); }
public void FileExistsReturnsFalseWhenBuildManagerFileExistsReturnsFalse() { // Arrange var engine = new TestableBuildManagerViewEngine(pathProvider: CreatePathProvider()); var buildManagerMock = new MockBuildManager("some path", false); engine.BuildManager = buildManagerMock; // Act bool result = engine.FileExists("some path"); // Assert Assert.False(result); }
public void FileExistsThrowsWhenBuildManagerThrowsNonHttpException() { // Arrange TestableWebFormViewEngine engine = new TestableWebFormViewEngine(); MockBuildManager buildManagerMock = new MockBuildManager(new InvalidOperationException("Some exception message.")); engine.BuildManager = buildManagerMock; // Act & Assert ExceptionHelper.ExpectInvalidOperationException( delegate { engine.PublicFileExists(null, "some path"); }, "Some exception message."); }
public void FileExistsReturnsTrueForExistingPath() { // Arrange var engine = new TestableBuildManagerViewEngine(); var buildManagerMock = new MockBuildManager("some path", typeof(object)); engine.BuildManager = buildManagerMock; // Act bool result = engine.FileExists("some path"); // Assert Assert.True(result); }
public void RenderWithNullWriterThrows() { // Arrange RazorView view = new RazorView(new ControllerContext(), "~/viewPath", null, false, Enumerable.Empty<string>()); Mock<ViewContext> viewContextMock = new Mock<ViewContext>(); MockBuildManager buildManager = new MockBuildManager("~/viewPath", typeof(object)); view.BuildManager = buildManager; // Act & Assert ExceptionHelper.ExpectArgumentNullException( () => view.Render(viewContextMock.Object, null), "writer" ); }
public void FileExistsThrowsWhenBuildManagerThrowsHttpParseException() { // Arrange TestableWebFormViewEngine engine = new TestableWebFormViewEngine(); MockBuildManager buildManagerMock = new MockBuildManager(new HttpParseException()); engine.BuildManager = buildManagerMock; // Act & assert ExceptionHelper.ExpectException <HttpParseException>( delegate { engine.PublicFileExists(null, "some path"); }); }
public void RenderWithUnsupportedTypeThrows() { // Arrange ViewContext context = new Mock<ViewContext>().Object; MockBuildManager buildManagerMock = new MockBuildManager("view path", typeof(int)); WebFormView view = new WebFormView(new ControllerContext(), "view path", null); view.BuildManager = buildManagerMock; // Act & Assert Assert.Throws<InvalidOperationException>( () => view.Render(context, null), "The view at 'view path' must derive from ViewPage, ViewPage<TModel>, ViewUserControl, or ViewUserControl<TModel>." ); }
public void FileExistsQueriesBuildManagerForFilesThatPathProviderDoesNotFindWhenRunningInPrecompiledNonUpdateableApp() { // Arrange string testPath = "/Path.txt"; var engine = new TestableBuildManagerViewEngine(pathProvider: CreatePathProvider("some random path")); engine.SetIsPrecompiledNonUpdateableSite(true); var buildManagerMock = new MockBuildManager(testPath, typeof(object)); engine.BuildManager = buildManagerMock; // Act bool result = engine.FileExists(testPath); // Assert Assert.True(result); }
public void RenderWithUnsupportedTypeThrows() { // Arrange ViewContext context = new Mock <ViewContext>().Object; MockBuildManager buildManagerMock = new MockBuildManager("view path", typeof(object)); RazorView view = new RazorView(new ControllerContext(), "view path", null, false, Enumerable.Empty <string>()); view.BuildManager = buildManagerMock; // Act & Assert Assert.Throws <InvalidOperationException>( () => view.Render(context, new Mock <TextWriter>().Object), "The view at 'view path' must derive from WebViewPage, or WebViewPage<TModel>." ); }
public void RenderWithViewUserControlAndMasterThrows() { // Arrange ViewContext context = new Mock <ViewContext>().Object; MockBuildManager buildManagerMock = new MockBuildManager("view path", typeof(StubViewUserControl)); WebFormView view = new WebFormView(new ControllerContext(), "view path", "master path"); view.BuildManager = buildManagerMock; // Act & Assert ExceptionHelper.ExpectException <InvalidOperationException>( () => view.Render(context, null), "A master name cannot be specified when the view is a ViewUserControl." ); }
public void RenderWithUnsupportedTypeThrows() { // Arrange ViewContext context = new Mock <ViewContext>().Object; MockBuildManager buildManagerMock = new MockBuildManager("view path", typeof(int)); WebFormView view = new WebFormView(new ControllerContext(), "view path", null); view.BuildManager = buildManagerMock; // Act & Assert ExceptionHelper.ExpectException <InvalidOperationException>( () => view.Render(context, null), "The view at 'view path' must derive from ViewPage, ViewPage<TModel>, ViewUserControl, or ViewUserControl<TModel>." ); }
public void RenderWithNullViewInstanceThrows() { // Arrange ViewContext context = new Mock <ViewContext>().Object; MockBuildManager buildManager = new MockBuildManager("view path", typeof(object), null); WebFormView view = new WebFormView("view path", null); view.BuildManager = buildManager; // Act & Assert ExceptionHelper.ExpectException <InvalidOperationException>( () => view.Render(context, null), "The view found at 'view path' could not be created." ); }
public void RenderWithNullViewInstanceThrows() { // Arrange ViewContext context = new Mock <ViewContext>().Object; MockBuildManager buildManager = new MockBuildManager("view path", compiledType: null); TestableBuildManagerCompiledView view = new TestableBuildManagerCompiledView(new ControllerContext(), "view path"); view.BuildManager = buildManager; // Act & Assert Assert.Throws <InvalidOperationException>( () => view.Render(context, new Mock <TextWriter>().Object), "The view found at 'view path' was not created." ); }
public void ViewPageActivatorDelegatesToDependencyResolverWhenActivatorResolverIsNull() { // Arrange var viewInstance = new object(); var controllerContext = new ControllerContext(); var buildManager = new MockBuildManager("view path", typeof(object)); var dependencyResolver = new Mock<IDependencyResolver>(MockBehavior.Strict); dependencyResolver.Setup(dr => dr.GetService(typeof(object))).Returns(viewInstance).Verifiable(); // Act TestableBuildManagerViewEngine engine = new TestableBuildManagerViewEngine(dependencyResolver: dependencyResolver.Object); engine.ViewPageActivator.Create(controllerContext, typeof(object)); // Assert dependencyResolver.Verify(); }
public void ViewCreationConsultsSetActivator() { // Arrange object viewInstance = new object(); Mock<IViewPageActivator> activator = new Mock<IViewPageActivator>(MockBehavior.Strict); ControllerContext controllerContext = new ControllerContext(); activator.Setup(a => a.Create(controllerContext, typeof(object))).Returns(viewInstance).Verifiable(); MockBuildManager buildManager = new MockBuildManager("view path", typeof(object)); BuildManagerCompiledView view = new TestableBuildManagerCompiledView(controllerContext, "view path", activator.Object) { BuildManager = buildManager }; // Act view.Render(new Mock<ViewContext>().Object, new Mock<TextWriter>().Object); // Assert activator.Verify(); }
public void RenderWithViewPageAndMasterRendersView() { // Arrange ViewContext context = new Mock<ViewContext>().Object; MockBuildManager buildManager = new MockBuildManager("view path", typeof(object)); Mock<IViewPageActivator> activator = new Mock<IViewPageActivator>(MockBehavior.Strict); ControllerContext controllerContext = new ControllerContext(); StubViewPage viewPage = new StubViewPage(); activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewPage); WebFormView view = new WebFormView(controllerContext, "view path", "master path", activator.Object); view.BuildManager = buildManager; // Act view.Render(context, null); // Assert Assert.AreEqual(context, viewPage.ResultViewContext); Assert.AreEqual("master path", viewPage.MasterLocation); }
public void SaveToCache_ReadFromCache_ReturnsNullIfTypesAreInvalid() { // // SAVING // // Arrange Type[] expectedTypes = new Type[] { typeof(object), typeof(string) }; TypeCacheSerializer serializer = new TypeCacheSerializer(); string cacheName = "testCache"; MockBuildManager buildManager = new MockBuildManager(); // Act TypeCacheUtil.SaveTypesToCache(cacheName, expectedTypes, buildManager, serializer); // Assert MemoryStream writeStream = buildManager.CachedFileStore[cacheName] as MemoryStream; Assert.NotNull(writeStream); byte[] streamContents = writeStream.ToArray(); Assert.NotEqual(0, streamContents.Length); // // READING // // Arrange MemoryStream readStream = new MemoryStream(streamContents); buildManager.CachedFileStore[cacheName] = readStream; // Act List<Type> returnedTypes = TypeCacheUtil.ReadTypesFromCache(cacheName, _ => false /* all types are invalid */, buildManager, serializer); // Assert Assert.Null(returnedTypes); }
public void GetFilteredTypesFromAssemblies_FallThrough() { // Arrange Type[] expectedTypes = new Type[] { typeof(TypeCacheValidFoo), typeof(TypeCacheValidBar) }; string cacheName = "testCache"; MockBuildManager buildManager = new MockBuildManager(); Predicate<Type> predicate = type => type.IsDefined(typeof(TypeCacheMarkerAttribute), true); // Act List<Type> returnedTypes = TypeCacheUtil.GetFilteredTypesFromAssemblies(cacheName, predicate, buildManager); // Assert CollectionAssert.AreEquivalent(expectedTypes, returnedTypes, "Correct types were not returned."); MemoryStream cachedStream = buildManager.CachedFileStore[cacheName] as MemoryStream; Assert.IsNotNull(cachedStream, "Stream was not created."); Assert.AreNotEqual(0, cachedStream.ToArray().Length, "Data was not written correctly."); }
public void GetFilteredTypesFromAssemblies_FallThrough() { // Arrange Type[] expectedTypes = new Type[] { typeof(TypeCacheValidFoo), typeof(TypeCacheValidBar) }; string cacheName = "testCache"; MockBuildManager buildManager = new MockBuildManager(); Predicate<Type> predicate = type => type.IsDefined(typeof(TypeCacheMarkerAttribute), true); // Act List<Type> returnedTypes = TypeCacheUtil.GetFilteredTypesFromAssemblies(cacheName, predicate, buildManager); // Assert Assert.Equal(expectedTypes, returnedTypes.ToArray()); MemoryStream cachedStream = buildManager.CachedFileStore[cacheName] as MemoryStream; Assert.NotNull(cachedStream); Assert.NotEqual(0, cachedStream.ToArray().Length); }
public void GetControllerTypeThatDoesntExist() { // Arrange RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData()); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b", "ns3a.ns3b", "ns4a.ns4b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly2"), Assembly.Load("MvcAssembly3"), Assembly.Load("MvcAssembly4") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type randomType1 = factory.GetControllerType(requestContext, "Cx"); Type randomType2 = factory.GetControllerType(requestContext, "Cy"); Type randomType3 = factory.GetControllerType(requestContext, "Foo.Bar"); Type randomType4 = factory.GetControllerType(requestContext, "C1Controller"); // Assert Assert.Null(randomType1); Assert.Null(randomType2); Assert.Null(randomType3); Assert.Null(randomType4); Assert.Equal(8, controllerTypeCache.Count); }
public void GetControllerTypeSearchesAllNamespacesAsLastResort() { // Arrange RequestContext requestContext = GetRequestContextWithNamespaces("ns3a.ns3b"); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c2Type = factory.GetControllerType(requestContext, "C2"); // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assert.NotNull(verifiedC2); Assert.Equal(verifiedC2, c2Type); Assert.Equal(2, controllerTypeCache.Count); }
public void GetControllerTypeForAssembliesWithSameTypeNamesInSameNamespaceThrows() { // Arrange RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData()); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly4") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Assert.Throws<InvalidOperationException>( delegate { factory.GetControllerType(requestContext, "C1"); }, "Multiple types were found that match the controller named 'C1'. This can happen if the route that services this request does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter." + Environment.NewLine + Environment.NewLine + "The request for 'C1' has found the following matching controllers:" + Environment.NewLine + "NS1a.NS1b.C1Controller" + Environment.NewLine + "NS1a.NS1b.C1Controller"); // Assert Assert.Equal(4, controllerTypeCache.Count); }
public void GetControllerTypeForAssembliesWithSameTypeNamesInDifferentNamespaces() { // Arrange RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData()); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType(requestContext, "C1"); Type c2Type = factory.GetControllerType(requestContext, "C2"); // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assert.NotNull(verifiedC1); Assert.NotNull(verifiedC2); Assert.Equal(verifiedC1, c1Type); Assert.Equal(verifiedC2, c2Type); Assert.Equal(4, controllerTypeCache.Count); }
public void GetControllerTypeForManyAssemblies() { // Arrange RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData()); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b", "ns3a.ns3b", "ns4a.ns4b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly2") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType(requestContext, "C1"); Type c2Type = factory.GetControllerType(requestContext, "C2"); Type c3Type = factory.GetControllerType(requestContext, "c3"); // lower case Type c4Type = factory.GetControllerType(requestContext, "c4"); // lower case // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assembly asm2 = Assembly.Load("MvcAssembly2"); Type verifiedC3 = asm2.GetType("NS3a.NS3b.C3Controller"); Type verifiedC4 = asm2.GetType("NS4a.NS4b.C4Controller"); Assert.NotNull(verifiedC1); Assert.NotNull(verifiedC2); Assert.NotNull(verifiedC3); Assert.NotNull(verifiedC4); Assert.Equal(verifiedC1, c1Type); Assert.Equal(verifiedC2, c2Type); Assert.Equal(verifiedC3, c3Type); Assert.Equal(verifiedC4, c4Type); Assert.Equal(4, controllerTypeCache.Count); }
public void GetControllerTypeForNoAssemblies() { // Arrange RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData()); DefaultControllerFactory factory = new DefaultControllerFactory(); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type controllerType = factory.GetControllerType(requestContext, "sometype"); // Assert Assert.Null(controllerType); Assert.Equal(0, controllerTypeCache.Count); }
public void RenderWithViewUserControlRendersView() { // Arrange ViewContext context = new Mock<ViewContext>().Object; MockBuildManager buildManager = new MockBuildManager("view path", typeof(object)); Mock<IViewPageActivator> activator = new Mock<IViewPageActivator>(MockBehavior.Strict); ControllerContext controllerContext = new ControllerContext(); StubViewUserControl viewUserControl = new StubViewUserControl(); activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewUserControl); WebFormView view = new WebFormView(controllerContext, "view path", null, activator.Object) { BuildManager = buildManager }; // Act view.Render(context, null); // Assert Assert.Equal(context, viewUserControl.ResultViewContext); }