public void ActivatorResolverAndDependencyResolverAreNeverCalledWhenViewPageActivatorIsPassedInContstructor()
        {
            // Arrange
            var controllerContext  = new ControllerContext();
            var expectedController = new Goodcontroller();

            Mock <IViewPageActivator> activator = new Mock <IViewPageActivator>();

            var resolverActivator = new Mock <IViewPageActivator>(MockBehavior.Strict);
            var activatorResolver = new Resolver <IViewPageActivator>
            {
                Current = resolverActivator.Object
            };

            var dependencyResolver = new Mock <IDependencyResolver>(MockBehavior.Strict);

            //Act
            var engine = new TestableBuildManagerViewEngine(
                activator.Object,
                activatorResolver,
                dependencyResolver.Object
                );

            //Assert
            Assert.Same(activator.Object, engine.ViewPageActivator);
        }
        public void FileExistsReturnsTrueForExistingPath_VPPRegistrationChanging()
        {
            AppDomainUtils.RunInSeparateAppDomain(() =>
            {
                // Arrange
                AppDomainUtils.SetAppData();
                new HostingEnvironment();

                // Expect null beforeProvider since hosting environment hasn't been initialized
                VirtualPathProvider beforeProvider = HostingEnvironment.VirtualPathProvider;
                string testPath = "/Path.txt";
                VirtualPathProvider afterProvider       = CreatePathProvider(testPath);
                Mock <VirtualPathProvider> mockProvider = Mock.Get(afterProvider);

                TestableBuildManagerViewEngine engine = new TestableBuildManagerViewEngine();

                // Act
                VirtualPathProvider beforeEngineProvider = engine.VirtualPathProvider;
                HostingEnvironment.RegisterVirtualPathProvider(afterProvider);

                bool result = engine.FileExists(testPath);
                VirtualPathProvider afterEngineProvider = engine.VirtualPathProvider;

                // Assert
                Assert.True(result);
                Assert.Equal(beforeProvider, beforeEngineProvider);
                Assert.Equal(afterProvider, afterEngineProvider);

                mockProvider.Verify();
                mockProvider.Verify(c => c.FileExists(It.IsAny <String>()), Times.Once());
            });
        }
        public void FileExistsReturnsTrueForExistingPath_VPPRegistrationChanging()
        {
            AppDomainUtils.RunInSeparateAppDomain(() =>
            {
                // Arrange
                AppDomainUtils.SetAppData();
                new HostingEnvironment();

                // Expect null beforeProvider since hosting environment hasn't been initialized
                VirtualPathProvider beforeProvider = HostingEnvironment.VirtualPathProvider;
                string testPath = "/Path.txt";
                VirtualPathProvider afterProvider = CreatePathProvider(testPath);
                Mock<VirtualPathProvider> mockProvider = Mock.Get(afterProvider);

                TestableBuildManagerViewEngine engine = new TestableBuildManagerViewEngine();

                // Act
                VirtualPathProvider beforeEngineProvider = engine.VirtualPathProvider;
                HostingEnvironment.RegisterVirtualPathProvider(afterProvider);

                bool result = engine.FileExists(testPath);
                VirtualPathProvider afterEngineProvider = engine.VirtualPathProvider;

                // Assert
                Assert.True(result);
                Assert.Equal(beforeProvider, beforeEngineProvider);
                Assert.Equal(afterProvider, afterEngineProvider);

                mockProvider.Verify();
                mockProvider.Verify(c => c.FileExists(It.IsAny<String>()), Times.Once());
            });
        }
        public void ViewPageActivatorConsultsSetActivatorResolver() {
            // Arrange
            Mock<IViewPageActivator> activator = new Mock<IViewPageActivator>();

            // Act
            TestableBuildManagerViewEngine engine = new TestableBuildManagerViewEngine(activator.Object);

            //Assert
            Assert.AreEqual(activator.Object, engine.ViewPageActivator);
        }
        public void ViewPageActivatorDelegatesToActivatorResolver() {
            // Arrange
            var activator = new Mock<IViewPageActivator>();
            var activatorResolver = new Resolver<IViewPageActivator> { Current = activator.Object };

            // Act
            TestableBuildManagerViewEngine engine = new TestableBuildManagerViewEngine(activatorResolver: activatorResolver);

            // Assert
            Assert.AreEqual(activator.Object, engine.ViewPageActivator);
        }
        public void ViewPageActivatorConsultsSetActivatorResolver()
        {
            // Arrange
            Mock <IViewPageActivator> activator = new Mock <IViewPageActivator>();

            // Act
            TestableBuildManagerViewEngine engine = new TestableBuildManagerViewEngine(activator.Object);

            //Assert
            Assert.Equal(activator.Object, engine.ViewPageActivator);
        }
        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 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 FileExistsReturnsTrueForExistingPath()
        {
            // Arrange
            string testPath = "/Path.txt";
            var engine = new TestableBuildManagerViewEngine(pathProvider: CreatePathProvider(testPath));

            // Act
            bool result = engine.FileExists(testPath);

            // Assert
            Assert.True(result);
        }
        public void FileExistsReturnsTrueForExistingPath()
        {
            // Arrange
            string testPath = "/Path.txt";
            var    engine   = new TestableBuildManagerViewEngine(pathProvider: CreatePathProvider(testPath));

            // Act
            bool result = engine.FileExists(testPath);

            // Assert
            Assert.True(result);
        }
        public void FileExistsReturnsFalseForNonExistentPath()
        {
            // Arrange
            string matchingPath = "/Path.txt";
            string nonMatchingPath = "/PathOther.txt";
            var engine = new TestableBuildManagerViewEngine(pathProvider: CreatePathProvider(matchingPath));

            // Act
            bool result = engine.FileExists(nonMatchingPath);

            // Assert
            Assert.False(result);
        }
Example #14
0
        public void FileExistsReturnsFalseForNonExistentPath()
        {
            // Arrange
            string matchingPath    = "/Path.txt";
            string nonMatchingPath = "/PathOther.txt";
            var    engine          = new TestableBuildManagerViewEngine(pathProvider: CreatePathProvider(matchingPath));

            // Act
            bool result = engine.FileExists(nonMatchingPath);

            // Assert
            Assert.False(result);
        }
        public void ViewPageActivatorDelegatesToActivatorResolver()
        {
            // Arrange
            var activator         = new Mock <IViewPageActivator>();
            var activatorResolver = new Resolver <IViewPageActivator> {
                Current = activator.Object
            };

            // Act
            TestableBuildManagerViewEngine engine = new TestableBuildManagerViewEngine(activatorResolver: activatorResolver);

            // Assert
            Assert.Equal(activator.Object, engine.ViewPageActivator);
        }
        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 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 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 FileExistsDoesNotQueryBuildManagerIfAppIsNotPrecompiledNonUpdateable()
        {
            // Arrange
            string testPath = "/Path.txt";
            var engine = new TestableBuildManagerViewEngine(pathProvider: CreatePathProvider("some random path"));
            var buildManagerMock = new Mock<MockBuildManager>();
            engine.BuildManager = buildManagerMock.Object;

            // Act
            bool result = engine.FileExists(testPath);

            // Assert
            buildManagerMock.Verify(b => b.FileExists(It.IsAny<string>()), Times.Never());
            Assert.False(result);
        }
        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 FileExistsDoesNotQueryBuildManagerIfAppIsNotPrecompiledNonUpdateable()
        {
            // Arrange
            string testPath         = "/Path.txt";
            var    engine           = new TestableBuildManagerViewEngine(pathProvider: CreatePathProvider("some random path"));
            var    buildManagerMock = new Mock <MockBuildManager>();

            engine.BuildManager = buildManagerMock.Object;

            // Act
            bool result = engine.FileExists(testPath);

            // Assert
            buildManagerMock.Verify(b => b.FileExists(It.IsAny <string>()), Times.Never());
            Assert.False(result);
        }
        public void ViewPageActivatorDelegatesToActivatorCreateInstanceWhenDependencyResolverReturnsNull()
        {
            // Arrange
            var controllerContext  = new ControllerContext();
            var buildManager       = new MockBuildManager("view path", typeof(NoParameterlessCtor));
            var dependencyResolver = new Mock <IDependencyResolver>();

            var engine = new TestableBuildManagerViewEngine(dependencyResolver: dependencyResolver.Object);

            // Act
            MissingMethodException ex = Assert.Throws <MissingMethodException>( // Depend on the fact that Activator.CreateInstance cannot create an object without a parameterless ctor
                () => engine.ViewPageActivator.Create(controllerContext, typeof(NoParameterlessCtor))
                );

            // Assert
            Assert.Contains("System.Activator.CreateInstance(", ex.StackTrace);
        }
        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 ViewPageActivatorDelegatesToActivatorCreateInstanceWhenDependencyResolverReturnsNull()
        {
            // Arrange
            var controllerContext  = new ControllerContext();
            var buildManager       = new MockBuildManager("view path", typeof(NoParameterlessCtor));
            var dependencyResolver = new Mock <IDependencyResolver>();

            var engine = new TestableBuildManagerViewEngine(dependencyResolver: dependencyResolver.Object);

            // Act & Assert, confirming type name and full stack are available in Exception
            // Depend on the fact that Activator.CreateInstance cannot create an object without a parameterless ctor
            MissingMethodException ex = Assert.Throws <MissingMethodException>(
                () => engine.ViewPageActivator.Create(controllerContext, typeof(NoParameterlessCtor)),
                "No parameterless constructor defined for this object. Object type 'System.Web.Mvc.Test.BuildManagerViewEngineTest+NoParameterlessCtor'.");

            Assert.Contains("System.Activator.CreateInstance(", ex.InnerException.StackTrace);
        }
        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();
        }
Example #26
0
        public void ViewPageActivatorDelegatesToActivatorCreateInstanceWhenDependencyResolverReturnsNull()
        {
            // Arrange
            var controllerContext  = new ControllerContext();
            var buildManager       = new MockBuildManager("view path", typeof(NoParameterlessCtor));
            var dependencyResolver = new Mock <IDependencyResolver>();

            var engine = new TestableBuildManagerViewEngine(dependencyResolver: dependencyResolver.Object);

            // Act
            Exception ex = ExceptionHelper.Record( // Depend on the fact that Activator.CreateInstance cannot create an object without a parameterless ctor
                () => engine.ViewPageActivator.Create(controllerContext, typeof(NoParameterlessCtor))
                );

            // Assert
            Assert.IsNotNull(ex);
            Assert.AreEqual(typeof(MissingMethodException), ex.GetType());
            StringAssert.Contains(ex.StackTrace, "System.Activator.CreateInstance(");
        }
        public void ViewPageActivatorDelegatesToActivatorCreateInstanceWhenDependencyResolverReturnsNull()
        {
            // Arrange
            var controllerContext = new ControllerContext();
            var buildManager = new MockBuildManager("view path", typeof(NoParameterlessCtor));
            var dependencyResolver = new Mock<IDependencyResolver>();

            var engine = new TestableBuildManagerViewEngine(dependencyResolver: dependencyResolver.Object);

            // Act & Assert, confirming type name and full stack are available in Exception
            // Depend on the fact that Activator.CreateInstance cannot create an object without a parameterless ctor
            MissingMethodException ex = Assert.Throws<MissingMethodException>(
                () => engine.ViewPageActivator.Create(controllerContext, typeof(NoParameterlessCtor)),
                "No parameterless constructor defined for this object. Object type 'System.Web.Mvc.Test.BuildManagerViewEngineTest+NoParameterlessCtor'.");
            Assert.Contains("System.Activator.CreateInstance(", ex.InnerException.StackTrace);
        }
        public void ViewPageActivatorDelegatesToActivatorCreateInstanceWhenDependencyResolverReturnsNull() {
            // Arrange
            var controllerContext = new ControllerContext();
            var buildManager = new MockBuildManager("view path", typeof(NoParameterlessCtor));
            var dependencyResolver = new Mock<IDependencyResolver>();

            var engine = new TestableBuildManagerViewEngine(dependencyResolver: dependencyResolver.Object);

            // Act
            Exception ex = ExceptionHelper.Record( // Depend on the fact that Activator.CreateInstance cannot create an object without a parameterless ctor
                () => engine.ViewPageActivator.Create(controllerContext, typeof(NoParameterlessCtor))
            );

            // Assert
            Assert.IsNotNull(ex);
            Assert.AreEqual(typeof(MissingMethodException), ex.GetType());
            StringAssert.Contains(ex.StackTrace, "System.Activator.CreateInstance(");
        }
        public void ActivatorResolverAndDependencyResolverAreNeverCalledWhenViewPageActivatorIsPassedInContstructor()
        {
            // Arrange
            var controllerContext = new ControllerContext();
            var expectedController = new Goodcontroller();

            Mock<IViewPageActivator> activator = new Mock<IViewPageActivator>();

            var resolverActivator = new Mock<IViewPageActivator>(MockBehavior.Strict);
            var activatorResolver = new Resolver<IViewPageActivator> { Current = resolverActivator.Object };

            var dependencyResolver = new Mock<IDependencyResolver>(MockBehavior.Strict);

            //Act
            var engine = new TestableBuildManagerViewEngine(activator.Object, activatorResolver, dependencyResolver.Object);

            //Assert
            Assert.Same(activator.Object, engine.ViewPageActivator);
        }
        public void ViewPageActivatorDelegatesToActivatorCreateInstanceWhenDependencyResolverReturnsNull()
        {
            // Arrange
            var controllerContext = new ControllerContext();
            var buildManager = new MockBuildManager("view path", typeof(NoParameterlessCtor));
            var dependencyResolver = new Mock<IDependencyResolver>();

            var engine = new TestableBuildManagerViewEngine(dependencyResolver: dependencyResolver.Object);

            // Act
            MissingMethodException ex = Assert.Throws<MissingMethodException>( // Depend on the fact that Activator.CreateInstance cannot create an object without a parameterless ctor
                () => engine.ViewPageActivator.Create(controllerContext, typeof(NoParameterlessCtor))
                );

            // Assert           
            Assert.Contains("System.Activator.CreateInstance(", ex.StackTrace);
        }