我正在与痣一起写一些单元测试。我在网上搜索,但我没有看到有关如何使用莫尔拦截对AppSettingsReader.GetValue的调用的任何反应。痣和AppSettingsReader?
有人能用莫尔斯做到这一点吗?或者,我是否被迫在自己的班级中隔离呼叫,我可以注射或模拟?理想情况下,有一种方法可以直接使用Moles来拦截调用,因为我们并不想修改要测试的代码。
谢谢!
我正在与痣一起写一些单元测试。我在网上搜索,但我没有看到有关如何使用莫尔拦截对AppSettingsReader.GetValue的调用的任何反应。痣和AppSettingsReader?
有人能用莫尔斯做到这一点吗?或者,我是否被迫在自己的班级中隔离呼叫,我可以注射或模拟?理想情况下,有一种方法可以直接使用Moles来拦截调用,因为我们并不想修改要测试的代码。
谢谢!
首先,我强烈建议转移到摩尔发行版本,被称为“假货和存根”,在.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 <appSettings> configuration section.
/// - or -
/// The value in the <appSettings> 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 <appSettings> configuration section.
/// - or -
/// The value in the <appSettings> 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;
}
}
}
我们不能使用F&M,因为VS 2012没有发布,我也不指望我们今年会使用它。 感谢有关存根的信息,如果我处于注射模型中,可能会有用。鉴于代码库,我不能使用存根来进行这组测试。 – EricR 2012-08-07 14:34:13
为了什么它的价值我结束了包裹呼叫: 公共类的AppSettings (Key,Type) { }返回新的AppSettingsReader()。GetValue(key,type); } } 我仍然想找到一个解决方案,因为我不想修改我想要放在测试下的代码。 – EricR 2012-07-31 17:51:04