2012-07-30 79 views
1

我正在與痣一起寫一些單元測試。我在網上搜索,但我沒有看到有關如何使用莫爾攔截對AppSettingsReader.GetValue的調用的任何反應。痣和AppSettingsReader?

有人能用莫爾斯做到這一點嗎?或者,我是否被迫在自己的班級中隔離呼叫,我可以注射或模擬?理想情況下,有一種方法可以直接使用Moles來攔截調用,因爲我們並不想修改要測試的代碼。

謝謝!

+0

爲了什麼它的價值我結束了包裹呼叫: 公共類的AppSettings (Key,Type) { }返回新的AppSettingsReader()。GetValue(key,type); } } 我仍然想找到一個解決方案,因爲我不想修改我想要放在測試下的代碼。 – EricR 2012-07-31 17:51:04

回答

0

首先,我強烈建議轉移到摩爾發行版本,被稱爲「假貨和存根」,在.NET 4.5/C#5 /的Visual Studio 2012

的System.Configurations命名空間與鼴鼠不兼容/假的類型,並且必須被樁住。要使用Moles框架創建存根,只需爲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; 
    } 
    } 
} 
+0

我們不能使用F&M,因爲VS 2012沒有發佈,我也不指望我們今年會使用它。 感謝有關存根的信息,如果我處於注射模型中,可能會有用。鑑於代碼庫,我不能使用存根來進行這組測試。 – EricR 2012-08-07 14:34:13