我有使用 excel 工作表的课程。如何编写测试来证明 Merge 方法被调用。
/// <summary>
/// Merges the cells together.
/// </summary>
/// <param name="ws">The worksheet.</param>
/// <param name="cellsToMerge">The cells to merge.</param>
/// <exception cref="System.ArgumentNullException">ws;Worksheet has to be defined</exception>
/// <exception cref="System.ArgumentException">Cells cannot contain null or empty string;cellsToMerge</exception>
public void MergeCellsTogether(Worksheet ws, string cellsToMerge)
{
if(ws==null) throw new ArgumentNullException("ws","Worksheet has to be defined");
if(string.IsNullOrEmpty(cellsToMerge))throw new ArgumentException("Cells cannot contain null or empty string", "cellsToMerge");
var cells = ws.Cells[cellsToMerge]; // failing to setup
ws.Range[cells].Merge();
}
现在我的测试是使用最小起订量
[TestMethod]
public void TestForMergingCellsTogether()
{
// assign
var cellsToMerge = "A1:C3";
// mock
var ws = new Mock<Worksheet>();
var range = new Mock<Range>();
ws.Setup(x => x.get_Range(It.IsAny<object>(), It.IsAny<object>())).Returns(range.Object);
// this is part that is giving my headake
ws.Setup(x => x.Cells[It.IsAny<object>(),It.IsAny<object>()]).Returns(range.Object);
range.Setup(x => x.Merge(It.IsAny<object>()));
// act
var ps = new RenderProcess("fileName");
ps.MergeCellsTogether(ws.Object, cellsToMerge);
// assert
range.VerifyAll();
}