0

我有使用 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();
        }
4

1 回答 1

0

我找到了我的问题的解决方案。我的方法不正确。

我更新(简化)的代码

/// <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();
于 2013-04-24T11:33:10.360 回答