如果您有一个需要测试的方法,它会获取SearchResults列表
public virtual void ProcessResults(IList<SearchResult> list)
{
//Code to tests here
}
Run Code Online (Sandbox Code Playgroud)
你如何模拟SearchResult列表?
注意:不允许使用低级注入框架(例如TypeMock).
我正在尝试对一些Active Directory代码进行单元测试,与此问题中概述的完全相同:
接受的答案建议为DirectoryEntry类实现一个包装器/适配器,我有:
public interface IDirectoryEntry : IDisposable
{
PropertyCollection Properties { get; }
}
public class DirectoryEntryWrapper : DirectoryEntry, IDirectoryEntry
{
}
Run Code Online (Sandbox Code Playgroud)
问题是我的mock 上的" Properties "属性IDirectoryEntry没有初始化.试图像这样设置模拟:
this._directoryEntryMock = new Mock<IDirectoryEntry>();
this._directoryEntryMock.Setup(m => m.Properties)
.Returns(new PropertyCollection());
Run Code Online (Sandbox Code Playgroud)
导致以下错误:
类型'System.DirectoryServices.PropertyCollection'没有定义构造函数
据我所知,当尝试仅使用内部构造函数实例化一个类时,会抛出此错误:
我试图为类编写一个包装器/适配器PropertyCollection但是没有公共构造函数我无法弄清楚如何从类中实例化或继承.
那么如何在类上模拟/设置" Properties "属性以DirectoryEntry进行测试呢?