Moles和AppSettingsReader?

时间:2012-07-30 11:33:37

标签: c# moles

我正在与Moles合作编写一些单元测试。我在线搜索但是我没有看到任何关于如何使用Moles拦截对AppSettingsReader.GetValue的调用的回复。

有没有人能够使用Moles做到这一点?或者我是否被迫在我自己的班级中隔离我可以注入或模拟的电话?理想情况下,有一种方法可以直接使用Moles来拦截调用,因为我们并不想真正修改我们想要测试的代码。

谢谢!

1 个答案:

答案 0 :(得分:0)

首先,我强烈建议在.NET 4.5 / C#5 / Visual Studio 2012中使用名为“Fakes and Stubs”的Moles发行版。

System.Configurations命名空间与Mole / Fake类型不兼容,必须存根。要使用Moles Framework创建存根,只需为System.Configuration.AppSettingsReader类型创建一个接口。 Moles编译器会自动将接口转换为Stub类型。

这是您可以添加到项目中的界面:

using System;

namespace YOUR_NAMESPACE_HERE
{
  /// <summary>
  /// IOC object for stubbing System.Configuration.AppSettingsReader.
  /// Provides a method for reading values of a particular type from
  /// the configuration.
  /// </summary>
  interface IAppSettingsReader
  {
    /// <summary>
    /// Gets the value for a specified key from the
    /// System.Configuration.ConfigurationSettings.AppSettings property
    /// and returns an object of the specified type containing the
    /// value from the configuration.
    /// </summary>
    /// <param name="key">The key for which to get the value.</param>
    /// <param name="type">The type of the object to return.</param>
    /// <returns>The value of the specified key</returns>
    /// <exception cref="System.ArgumentNullException">key is null.
    /// - or -
    /// type is null.</exception>
    /// <exception cref="System.InvalidOperationException">key does
    /// not exist in the &lt;appSettings&gt; configuration section.
    /// - or -
    /// The value in the &lt;appSettings&gt; configuration section
    /// for key is not of type type.</exception>
    public object GetValue(string key, Type type);
  }
}

这也是一个存根类:

using System;
using System.Configuration;

namespace YOUR_NAMESPACE_HERE
{
  /// <summary>
  /// Production stub for System.Configuration.AppSettingsReader.
  /// Provides a method for reading values of a particular type from
  /// the configuration.
  /// </summary>
  public class AppSettingsReaderStub : IAppSettingsReader
  {
    /// <summary>
    /// Gets the value for a specified key from the
    /// System.Configuration.ConfigurationSettings.AppSettings property
    /// and returns an object of the specified type containing the value
    /// from the configuration.
    /// </summary>
    /// <param name="key">The key for which to get the value.</param>
    /// <param name="type">The type of the object to return.</param>
    /// <returns>The value of the specified key</returns>
    /// <exception cref="System.ArgumentNullException">key is null.
    /// - or -
    /// type is null.</exception>
    /// <exception cref="System.InvalidOperationException">key does not
    /// exist in the &lt;appSettings&gt; configuration section.
    /// - or -
    /// The value in the &lt;appSettings&gt; configuration section for
    /// key is not of type type.</exception>
    public object GetValue(string key, Type type)
    {
      var reader = new AppSettingsReader();
      object result = reader.GetValue(key, type);
      return result;
    }
  }
}