SpecFlow FeatureSetup抛出System.Reflection.ReflectionTypeLoadException

时间:2017-04-28 18:14:20

标签: c# specflow

SpecFlow将在FeatureSetup方法中抛出System.Reflection.ReflectionTypeLoadException,它不提供有关异常的任何信息,并会要求您检索LoaderExceptions属性(如下所示)。在生成的FeatureSetup方法的开头设置调试中断以查看问题不会触发中断。我怎样才能找到这个问题的根源?

错误:无法加载一个或多个请求的类型。检索LoaderExceptions属性以获取更多信息。

1 个答案:

答案 0 :(得分:0)

在生成的FeatureSetup方法中的所有代码周围添加一个try / catch块,并添加Console.WriteLine以输出每个LoaderException,如下所示。运行测试时,LoaderException消息将显示在NUnit GUI屏幕上。 StackFlow链接How to retrieve the LoaderException property?非常有用。

        try
        {
            testRunner = TechTalk.SpecFlow.TestRunnerManager.GetTestRunner();
            TechTalk.SpecFlow.FeatureInfo featureInfo = new TechTalk.SpecFlow.FeatureInfo(new System.Globalization.CultureInfo("en-US"), "MyTest", null, ProgrammingLanguage.CSharp, ((string[])(null)));
            testRunner.OnFeatureStart(featureInfo);
        }
        catch (Exception e)
        {
            var typeLoadException = e as ReflectionTypeLoadException;
            var loaderExceptions = typeLoadException.LoaderExceptions;
            foreach (Exception le in loaderExceptions)
                Console.WriteLine("LoaderException Msg = {0}", le.Message);
        }