gpt4 book ai didi

c# - 模拟只读索引器属性

转载 作者:太空狗 更新时间:2023-10-30 01:01:25 27 4
gpt4 key购买 nike

我想知道如何模拟一个索引属性,关于这个有很多问题:

  1. Moq an indexed property and use the index value in the return/callback
  2. How to MOQ an Indexed property
  3. How to Moq Setting an Indexed property

等。但就我而言,这增加了复杂性。索引属性是只读的。所以,我需要能够测试一段执行以下操作的代码

if (workbook.Worksheets.Cast<IWorksheet>().Any(
ws => ws.Name.CompareNoCase(Keywords.Master)))
{
...
}

我们有以下类结构

public interface IWorkbook
{
IWorksheets Worksheets { get; }
}

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

public interface IWorksheet
{
string Name { get; set; }
}

所以在我的测试方法中,我已经尝试(但失败了)通过覆盖 GetEnumerator() 方法来做到这一点,因为这正是 Cast() 所调用的;我这样做如下:

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);
}

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

// I can't do this as per the threads referenced above, as the property is read only.
//for (int i = 0; i < worksheetMockList.Count; ++i)
//mockWorkbook.SetupGet(p => p.Worksheets[i] = worksheetMockList[i])...

如何模拟我的 workbook.Worksheets 只读迭代器属性?


我多了一层抽象。我需要将 IWorkbook 添加到 IWorkbooks 集合(就像我们对 IWorksheets 所做的那样)。我没有把它放在原来的问题中,因为它只是在做与我们为 IWorksheets 所做的相同的事情,但是它不起作用。接口(interface)是

public interface IWorkbookSet
{
...
IWorkbooks Workbooks { get; }
}

public interface IWorkbooks : IEnumerable
{
IWorkbook this[int index] { get; }
IWorkbook this[string name] { get; }
int Count { get; }
...
}

因此,为了尝试处理这个问题,我按照以下很好的答案以以下方式进行 mock 。但是,下面的循环没有按预期工作。

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


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

worksheetMockList.Add(tmpWorksheetMock.Object);
}
var mockWorksheets = new Mock<IWorksheets>();
mockWorksheets.Setup(m => m[It.IsAny<int>()]).Returns<int>(index => worksheetMockList[index]);
mockWorksheets.Setup(m => m.GetEnumerator()).Returns(worksheetMockList.GetEnumerator());

mockWorkbook
.Setup(p => p.Worksheets)
.Returns(mockWorksheets.Object);
mockWorkbook.Setup(p => p.Name).Returns("Name");
mockWorkbook.Setup(p => p.FullName).Returns("FullName");

// This works.
foreach (IWorksheet ws in mockWorkbook.Object.Worksheets)
Trace.WriteLine(ws.Name);

mockWorkbookSet = new Mock<IWorkbookSet>();
var mockWorkbooks = new Mock<IWorkbooks>();
List<IWorkbook> workbookMockList = new List<IWorkbook>() { mockWorkbook.Object };

mockWorkbooks.Setup(m => m[It.IsAny<int>()]).Returns<int>(index => workbookMockList[index]);
mockWorkbooks.Setup(m => m.GetEnumerator()).Returns(workbookMockList.GetEnumerator());
mockWorkbookSet
.Setup(p => p.Workbooks)
.Returns(mockWorkbooks.Object);

// Count is zero here??
foreach (IWorkbook wb in mockWorkbookSet.Object.Workbooks)
Trace.WriteLine(wb.Worksheets.Count);

非常感谢。


编辑 #2:使用您的代码我有一些有趣的行为...

// Setup test.
var workbookSet = mockWorkbookSet.Object;
var actual = workbookSet
.Workbooks[expectedWorkBooksIndex]
.Worksheets[expectedWorkSheetIndex];

// This prints "A" - GOOD!
Trace.WriteLine("Actual " + actual.Name);

// This passes.
Assert.AreEqual(expected, actual);

// This works.
foreach (IWorksheet ws in mockWorkbook.Object.Worksheets)
Trace.WriteLine(ws.Name);

// This works.
Trace.WriteLine(mockWorkbookSet.Object.Workbooks[0].Name);

// This does not write anything - WHY?
foreach (IWorksheet ws in mockWorkbookSet.Object.Workbooks[0].Worksheets.Cast<IWorksheet>())
Trace.WriteLine(ws.Name);

// This fails.
foreach (IWorkbook workbook in workbookSet.Workbooks.Cast<IWorkbook>())
Assert.IsTrue(workbook.Worksheets.Count > 0);

最佳答案

通过使用

mockWorkSheets
.Setup(m => m[It.IsAny<int>()])
.Returns<int>(index => worksheetMockList[index]);

哪里It.IsAny<int>().Returns<int>(index => ...)可以访问传递给模拟的值您可以访问 .Returns 中的索引方法。

下面的例子展示了如何设置模拟

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

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

fakeWorkSheetsList.Add(tmpMock);
}

var mockWorkSheets = new Mock<IWorksheets>();
mockWorkSheets.Setup(m => m[It.IsAny<int>()])
.Returns<int>(index => fakeWorkSheetsList[index]);
mockWorkSheets.Setup(m => m.GetEnumerator())
.Returns(() => fakeWorkSheetsList.GetEnumerator());
//Assuming a Count property exists
mockWorkSheets.Setup(m => m.Count).Returns(fakeWorkSheetsList.Count);

//Workbook
var mockWorkbook = new Mock<IWorkbook>();
mockWorkbook.Setup(p => p.Name).Returns("Name");
mockWorkbook.Setup(p => p.FullName).Returns("FullName");
mockWorkbook.Setup(p => p.Worksheets).Returns(mockWorkSheets.Object);

//Workbooks
var fakeWorkbooksList = new List<IWorkbook>() { mockWorkbook.Object };

var mockWorkbooks = new Mock<IWorkbooks>();
mockWorkbooks.Setup(m => m[It.IsAny<int>()])
.Returns<int>(index => fakeWorkbooksList[index]);
mockWorkbooks.Setup(m => m.GetEnumerator())
.Returns(() => fakeWorkbooksList.GetEnumerator());
mockWorkbooks.Setup(m => m.Count).Returns(fakeWorkbooksList.Count);

//WorkbookSet
var mockWorkbookSet = new Mock<IWorkbookSet>();
mockWorkbookSet.Setup(m => m.Workbooks).Returns(mockWorkbooks.Object);

var workbookSet = mockWorkbookSet.Object;

var expectedWorkBooksIndex = 0;
var expectedWorkSheetIndex = 1;
var expected = fakeWorkSheetsList[expectedWorkSheetIndex];

//Act
var actual = workbookSet
.Workbooks[expectedWorkBooksIndex]
.Worksheets[expectedWorkSheetIndex];

//Assert
Assert.AreEqual(expected, actual);

foreach (IWorkbook workbook in workbookSet.Workbooks) {
Assert.IsTrue(workbook.Worksheets.Count > 0);
}

}

关于c# - 模拟只读索引器属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39129161/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com