【问题标题】:How to throw an exception from the method with return type as void using Microsoft Fakes on VS 2013如何在 VS 2013 上使用 Microsoft Fakes 从返回类型为 void 的方法中抛出异常
【发布时间】:2015-08-05 23:38:59
【问题描述】:

我有以下代码我正在单元测试。

    public static MyConfiguration GetConfig(string sectionName)
    {
        if (!Initialized)
        {
            try
            {
                InitializeEnv(); // Method is present but no implementation yet.
            }
            finally
            {
                Initialized = true;
                if (configurationReader == null)
                {
                    configurationReader = ConfigurationValueReader.ConfigurationReader();
                }
            }
        }
        return (MyConfiguration)configurationReader.ReadSection(sectionName, new MyConfigurationHandler());
    }

我想从方法 InitializeEnv() 中抛出一个异常,该方法使用 shims 或 stubs 在与上述方法相同的类中定义。该方法 InitializeEnv() 目前在其中没有实现导致数据例外。有没有办法我仍然可以在这里使用 Shims 或 Stubs 从该方法中抛出异常?

【问题讨论】:

    标签: c# stub microsoft-fakes shim


    【解决方案1】:

    能够填充它,但我需要在我的测试方法中添加一个 ExpectedException 属性,因为 Catch 块不存在。

     ShimListenerConfiguration.InitializeEnv = () =>
                {
                    throw new Exception();
                };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多