如何在 VS 2013 上使用 Microsoft Fakes 从 return 类型为 void 的方法中抛出异常

How to throw an exception from the method with return type as void using Microsoft Fakes on VS 2013

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

    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() 中抛出异常,该方法定义在与上述方法相同的 class 中,使用垫片或 stubs.This 方法 InitializeEnv() 没有此时它内部的实现导致数据异常。有没有办法我仍然可以在此处使用垫片或存根从该方法中抛出异常?

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

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