/// <summary>
        /// Creates a new test extension.
        /// </summary>
        /// <typeparam name="TTestExtension">Type of test extension.</typeparam>
        /// <typeparam name="TType">Type the test extension is for.</typeparam>
        /// <param name="type">Object which is passed to the test extension constructor.</param>
        /// <param name="currentTestExtension">TestExtension from where this is called.</param>
        /// <returns>Test extension created.</returns>
        public static TTestExtension Create <TTestExtension, TType>(TType type, TestExtension currentTestExtension)
            where TType : class
            where TTestExtension : TestExtension
        {
            Type testExtensionType = typeof(TTestExtension);

            object[]       parameters = { type };
            TTestExtension extension  = (TTestExtension)testExtensionType.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, parameters, CultureInfo.InvariantCulture);
            LifeTimeManagmentServiceTestExtension lifeTimeManagmentService = currentTestExtension.Container.GetFirstTestExtension <LifeTimeManagmentServiceTestExtension>();

            // This is a temporary workaround to get any test extension to be able to get the lifetime managment service till we implement our own test extension factory.
            if (extension.Container == null)
            {
                extension.Container = currentTestExtension.Container;
            }

            lifeTimeManagmentService.AddToCompositionContainer(extension);

            return(extension);
        }
Example #2
0
        /// <summary>
        /// Responsible for creating the build manager.
        /// For MSBuild backend testing BuildManager is the Entry point for all tests. That is - the environment
        /// for testing starts with the BuildManager. Configuration can specify which components should be mocked
        /// and which test extension should be attached to which component.
        /// </summary>
        /// <returns>TextExtensions created.</returns>
        protected override TestExtensionContainer Generate()
        {
            List <TestExtension> testExtensions = new List <TestExtension>();

            // To workaround the problem where extensions derived out from Testextension does not have a LifeTimeManagmentService.
            LifeTimeManagmentServiceTestExtension lifetimeServiceExtension = new LifeTimeManagmentServiceTestExtension(LifetimeService);

            LifetimeService.Compose(lifetimeServiceExtension);
            testExtensions.Add(lifetimeServiceExtension);

            // Create the build manager and the associated test extension first.
            BuildManagerTestExtension buildManagerTestExtension = new BuildManagerTestExtension(BuildManager.DefaultBuildManager);

            LifetimeService.Compose(buildManagerTestExtension);
            testExtensions.Add(buildManagerTestExtension);

            // When the BuildManager is created it registers a default set of components.
            // Loop through each of the components that we want to mock and then replace the component in the BuildManager.
            foreach (KeyValuePair <ComponentType, string> componentTypePair in this.Configuration.ComponentsToMock)
            {
                buildManagerTestExtension.ReplaceRegisterdFactory(GetBuildComponentTypeFromComponentType(componentTypePair.Key.ToString()), this.CreateMockComponent);
            }

            // Loop through each of the components that we want to wrap with a test extension - create the test extension and aggregate the internal component.
            // This component could be a mock that we create above or the real implementation.
            foreach (KeyValuePair <ComponentType, string> componentTypePair in this.Configuration.TestExtensionForComponents)
            {
                TestExtension extension = CreateTestExtensionForComponent(componentTypePair.Key.ToString(), componentTypePair.Value, buildManagerTestExtension);
                LifetimeService.Compose(extension);
                testExtensions.Add(extension);
            }

            TestExtensionContainer testContainer = new TestExtensionContainer(testExtensions);

            return(testContainer);
        }