public void ShouldCreateObject() { TestApplication application = new TestApplication(); object result = application.CreateObject(typeof(KeyValuePair <int, int>), 1, 2); Assert.Equal(new KeyValuePair <int, int>(1, 2), result); }
/// <summary> /// Creates an instance of corresponding class. /// </summary> /// <param name="testType">Type of the test to instantiate.</param> /// <param name="arguments">The arguments.</param> /// <returns>Instance of the class.</returns> public static LiveTest Instantiate(Type testType, params object[] arguments) { if (HostingEnvironment.IsHosted) { return(CreateUninitializedInstance(testType)); } if (InstantiatedByProxy(testType, arguments)) { return(Intercept(testType, null)); } MethodInfo getDefaultTestApplicationManagerMethod = Utility.GetInheritedMethod(testType, GetDefaultTestApplicationManagerName, new[] { typeof(Type), typeof(object[]) }); MethodInfo getDefaultApplicationHostMethod = Utility.GetInheritedMethod(testType, GetDefaultApplicationHostName, new[] { typeof(Type), typeof(object[]) }); if (getDefaultTestApplicationManagerMethod == null) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Cannot create an instance of type '{0}' because there is no '{1}' static method defined in its inheritance hierarchy. See '{2}' methods for an example of corresponding method signature.", testType.AssemblyQualifiedName, GetDefaultTestApplicationManagerName, typeof(LiveTest).AssemblyQualifiedName)); } if (getDefaultApplicationHostMethod == null) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Cannot create an instance of type '{0}' because there is no '{1}' static method defined in its inheritance hierarchy. See '{2}' methods for an example of corresponding method signature.", testType.AssemblyQualifiedName, GetDefaultApplicationHostName, typeof(LiveTest).AssemblyQualifiedName)); } object[] allArguments = { testType, arguments }; TestApplicationManager testApplicationManager = (TestApplicationManager)getDefaultTestApplicationManagerMethod.Invoke(null, allArguments); if (testApplicationManager == null) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Failed to get an instance of '{0}'.", typeof(TestApplicationManager).AssemblyQualifiedName)); } TestApplicationHost host = (TestApplicationHost)getDefaultApplicationHostMethod.Invoke(null, allArguments); if (host == null) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Failed to get an instance of '{0}'.", typeof(TestApplicationHost).AssemblyQualifiedName)); } TestApplication testApplication = testApplicationManager.StartApplication(host); if (testApplication == null) { throw new InvalidOperationException("Failed to get application to execute tests in."); } return((LiveTest)testApplication.CreateObject(testType, arguments)); }