用最小起订量模拟 System.Collections.Generic.IList

Mocking System.Collections.Generic.IList with Moq

我正在努力为我的 class 附件处理器编写单元测试。 我的目标是测试所有方法并模拟例如 IList FileList。

    public class AttachmentsProcessor
    {
      public IList<IFileInfo> FileList { get; set; }

    public AttachmentsProcessor(IList<IFileInfo> FileList)
    {
       ...
    }

    public void RemoveAttachment(int index)
    {
     ...
    }

    public long GetTotalFilesSize()
    {
     ...
    }

    public void GetFilesFromDialog(IOpenFileDialog2 openFileDialog1)
    {
     ...
    }   
}

接口:

public interface IFileProcessor
{
    IList<IFileInfo> FileList { get; set; }

    void RemoveAttachment(int index);

    long GetTotalFilesSize();

    void GetFilesFromDialog(IOpenFileDialog2 openFileDialog1);
}

我的问题已经解决了。我的问题是我不知道如何为我的 Class AttachmentProcessor:IFileProcessor 和 List 制作模拟。 我最终得到了这个解决方案并且它有效!感谢大家的回复。

        var mockFileProcessor = new Mock<IFileProcessor>(); //here is how I mocked my AttachmentProcessor
        var mockAttachmentInfo = new Mock<IFileInfo>(); ///here is how I mocked my AttachmentInfo
        mockAttachmentInfo.Setup(m => m.Length).Returns(() => 200);
        mockFileProcessor.Setup(m => m.FileList).Returns(() => new List<IFileInfo> 
        {
         mockAttachmentInfo.Object,
         mockAttachmentInfo.Object,
         mockAttachmentInfo.Object, 
         mockAttachmentInfo.Object,
         mockAttachmentInfo.Object 
        }); /// and this is the part where I mocked my IList<AttachmentInfo>

我最终找到了这个解决方案并且它奏效了!感谢大家的回复