1

Moles と協力して単体テストを作成しています。オンラインで検索しましたが、Moles を使用して AppSettingsReader.GetValue への呼び出しをインターセプトする方法についての応答がありません。

Molesを使用してこれを行うことができた人はいますか? それとも、注入またはモックできる自分のクラスで呼び出しを分離することを余儀なくされていますか? テスト対象のコードを実際に変更したくないため、Moles を直接使用して呼び出しをインターセプトする方法があるのが理想的です。

ありがとう!

4

1 に答える 1

0

まず、.NET 4.5 / C# 5 / Visual Studio 2012 の "Fakes and Stubs" と呼ばれる Moles のリリース バージョンに移行することを強くお勧めします。

System.Configurations 名前空間は Mole/Fake 型と互換性がないため、スタブ化する必要があります。Moles フレームワークを使用してスタブを作成するには、System.Configuration.AppSettingsReader 型のインターフェイスを作成するだけです。Moles コンパイラは、インターフェイスを自動的にスタブ型に変換します。

プロジェクトに追加できるインターフェイスは次のとおりです。

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;
    }
  }
}
于 2012-07-31T20:13:22.950 に答える