我的代码如下所示:
public class FileFormatLookup : Dictionary<string,Func<string>>
{
private Workbook workbook;
private MemoryStream memoryStream;
public FileFormatLookup(Workbook workbook, MemoryStream memoryStream)
{
this.workbook = workbook;
this.memoryStream = memoryStream;
Add();
}
private void Add()
{
base.Add("xls", () =>
{
workbook.Save(memoryStream, SaveFormat.Excel97To2003);
return "ms-excel";
});
}
}
我正在尝试编写如下测试:
class FileFormatLookupTest
{
FileFormatLookup fileFormatLookup;
private Workbook workbook;
private MemoryStream memoryStream;
[SetUp]
public void SetUp()
{
fileFormatLookup = new FileFormatLookup(workbook, memoryStream);
workbook = MockRepository.GenerateMock<Workbook>();
memoryStream = MockRepository.GenerateMock<MemoryStream>();
}
[TearDown]
public void TearDown()
{
workbook.VerifyAllExpectations();
}
[Test]
public void ShouldHaveXlsKey()
{
var xlsResult = fileFormatLookup["xls"].Invoke();
workbook.AssertWasCalled(x=>x.Save(memoryStream,SaveFormat.Excel97To2003));
}
}
这是在字典中测试函子的正确方法吗?它抛出此错误:
System.InvalidOperationException : No expectations were setup to be verified, ensure that the method call in the action is a virtual (C#) / overridable (VB.Net) method call
如何测试返回函数的字典?是否可以?我可以断言返回的字符串值,但不能断言方法调用。