模拟测试合并单元格在excel中

本文关键字:excel 单元格 测试 合并 模拟 | 更新日期: 2023-09-27 18:10:45

我有类工作与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();
        }

模拟测试合并单元格在excel中

我已经找到解决问题的方法了。我的方法是错误的。

我的更新(简化)代码

/// <summary>
        /// Merges the cells together.
        /// </summary>
        /// <param name="worksheet">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 worksheet, string cellsToMerge)
        {
            if(worksheet==null) throw new ArgumentNullException("worksheet","Worksheet has to be defined");
            if(string.IsNullOrEmpty(cellsToMerge))throw new ArgumentException("Cells cannot contain null or empty string", "cellsToMerge");
            worksheet.Range[cellsToMerge].Merge();
        }

和我的测试方法:

 // 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);
                range.Setup(x => x.Merge(It.IsAny<object>()));
                // act 
                var process = new RenderExcel();
                process.CreateExcelWorkSheet("fileName");
                process.MergeCellsTogether((Worksheet)ws.Object, cellsToMerge);
                // assert
                range.VerifyAll();