使用Moq模拟自定义集合

时间:2016-08-24 15:12:19

标签: c# unit-testing moq .net-4.6

我在第三方库中有以下类型

public interface IWorkbook : IPrintable
{
    ...
    IWorksheets Worksheets { get; }
}

工作表界面是

public interface IWorksheets : IEnumerable
{
    IWorksheet this[int index] { get; }
    IWorksheet this[string name] { get; }
    IWorkbookSet WorkbookSet { get; }
    IWorksheet Add();
    IWorksheet AddAfter(ISheet sheet);
    IWorksheet AddBefore(ISheet sheet);
    bool Contains(IWorksheet worksheet);
}

我有一个方法,我想进行单元测试,需要IWorkbook并迭代所包含的工作表。我的问题是如何使用Moq为IWorksheets创建模拟集合。 IWorksheet接口是

public IWorksheet
{
    ...
    public Name { get; set; } // This is the only property I am interested in.
}

那么如何生成假IWorkbookIWorksheets的假IWorksheet

我已经开始了

[TestInitialize]
public void Initialize()
{
    List<string> fakeSheetNames = new List<string>()
    {
        "Master",
        "A",
        "B",
        "C",
        "__ParentA", 
        "D",
        "wsgParentB", 
        "E",
        "F",
        "__ParentC",
        "__ParentD",
        "G"
    };

    List<IMock<IWorksheet>> sheetMockList = new List<IMock<IWorksheet>>();
    foreach (string name in fakeSheetNames)
    {
        Mock<IWorksheet> tmpMock = new Mock<IWorksheet>();
        tmpMock.Setup(p => p.Name).Returns(name);
        sheetMockList.Add(tmpMock);
    }

    var mockWorksheets = new Mock<IWorksheets>();
    mockWorksheets.Setup(p => p).Returns(sheetMockList);
    ...
}

但我不能这样做(显然)

  

无法转换为&#39; System.Collections.Generic.List&gt;&#39;到#SpreadsheetGear.IWorksheets&#39;

如何模拟IWorksheets集合?

所以我现在有以下代码根据下面的答案创建我的模拟

    [TestClass]
public class WorkbookStrucutreProviderTests
{
    private Mock<IWorkbookSet> mockWorkbookSet;
    private readonly List<string> parentPrefixes = new List<string>() { "__", "wsg" };

    [TestInitialize]
    public void Initialize()
    {
        List<string> fakeSheetNames = new List<string>()
        {
            "Master",
            "A",
            "B",
            "C",
            "__ParentA", 
            "D",
            "wsgParentB", 
            "E",
            "F",
            "__ParentC",
            "__ParentD",
            "G"
        };

        List<IWorksheet> worksheetMockList = new List<IWorksheet>();
        foreach (string name in fakeSheetNames)
        {
            Mock<IWorksheet> tmpMock = new Mock<IWorksheet>();
            tmpMock.Setup(p => p.Name).Returns(name);
            tmpMock.Setup(p => p.Visible)
                .Returns(parentPrefixes.Any(p => name.StartsWith(p)) ? 
                    SheetVisibility.Hidden : 
                    SheetVisibility.Visible);

            worksheetMockList.Add(tmpMock.Object);
        }

        List<IWorkbook> workbookMockList = new List<IWorkbook>();
        Mock<IWorkbook> mockWorkbook = new Mock<IWorkbook>();
        mockWorkbook
            .Setup(p => p.Worksheets.GetEnumerator())
            .Returns(worksheetMockList.GetEnumerator());
        workbookMockList.Add(mockWorkbook.Object);

        mockWorkbookSet = new Mock<IWorkbookSet>();
        mockWorkbookSet
            .Setup(p => p.Workbooks.GetEnumerator())
            .Returns(workbookMockList.GetEnumerator());
    }

    [TestMethod]
    public async Task StrucutreGenerationAsyncTest()
    {
        WorkbookStructureProvider provider = new WorkbookStructureProvider();
        await provider.GenerateWorkbookStructureAsync(mockWorkbookSet.Object);

        foreach (var item in provider.Structure)
        {
            Trace.WriteLine("--" + item.Name);
            if (item.HasChildren)
            {
                foreach (var child in item.Children)
                {
                    Trace.WriteLine("-- --" + child.Name);
                }
            }
        }
    }

但是在GenerateWorkbookStructureAsync()方法中我有这段代码

bool IsUserCostWorkbook = false;
if (workbook.Worksheets.Cast<IWorksheet>().Any(
    ws => ws.Name.CompareNoCase(Keywords.Master)))
{
    // TODO Extra check for UserCost template.
    IsUserCostWorkbook = true;          
}

此处workbook.Worksheets集合为空。我以为我的模拟GetEnumerator会处理这个问题;它没有。

那么如何模仿IWorksheets以便我可以执行以下操作?

foreach (var ws in workbook.Worksheets.Cast<IWorksheet>())
{
    ...
}

2 个答案:

答案 0 :(得分:2)

以下示例在测试时通过

[TestMethod]
public void Mock_Custom_Collection_Using_Moq() {
    //Arrange
    var parentPrefixes = new List<string>() { "__", "wsg" };
    var fakeSheetNames = new List<string>(){
        "Master",
        "A",
        "B",
        "C",
        "__ParentA", 
        "D",
        "wsgParentB", 
        "E",
        "F",
        "__ParentC",
        "__ParentD",
        "G"
    };

    var worksheetMockList = new List<IWorksheet>();
    foreach (string name in fakeSheetNames) {
        var worksheet = Mock.Of<IWorksheet>();
        worksheet.Name = name;
        worksheet.Visible = parentPrefixes.Any(p => name.StartsWith(p)) ?
                SheetVisibility.Hidden :
                SheetVisibility.Visible;

        worksheetMockList.Add(worksheet);
    }

    var mockWorkbook = new Mock<IWorkbook>();
    mockWorkbook
        .Setup(p => p.Worksheets.GetEnumerator())
        .Returns(() => worksheetMockList.GetEnumerator());

    var workbook = mockWorkbook.Object;

    //Act
    bool IsUserCostWorkbook = false;
    if (workbook.Worksheets.Cast<IWorksheet>()
        .Any(ws => ws.Name.Equals("Master", StringComparison.InvariantCultureIgnoreCase))) {
        IsUserCostWorkbook = true;
    }

    //Assert            
    Assert.IsTrue(IsUserCostWorkbook);
}

答案 1 :(得分:1)

抱歉伪代码:

var fakeWorksheet = new Mock<IWorksheet>();
//You can use AutoFixture here to auto-populate properties or you can set only required props
fakeWorksheet.Setup(p => p.Name).Returns("TestName");
var worksheetsMock = new Mock<IWorksheets>()
//here mock some members that you need
worksheetsMock.Setup(w => w.Add()).Returns(fakeWorksheet.Object);
var workbookMock = new Mock<IWorkbook>();
workbookMock.Setup(w => w.Worksheets).Returns(worksheetsMock.Object);