Unit Testing a Method that Interacts with the Excel Interop Library

I'm trying to become better at writing cleaner code and developing automated tests for methods in applications so as to test the application quicker and write more flexible and extensible code. While I think I understand the basics of Unit Testing, I'm having trouble developing a test for a specific method. I've recently taken an online course on Unit Testing and I learned that in order to write Unit Tests for methods that touch external resources, you should create a class that implements the code that interacts with that external resource and extract an interface for that class so the a Mock instance can be created from the extracted interface. I think using the Excel Interop library qualifies as touching an external resource and the method I'm working on right now is meant to save an Excel file to the user's computer. The code for this method can be seen below.

 public void Save() { if (DoesFileExistInDirectory()) { semesterGradeInformationWorkbook.Save(); } else { semesterGradeInformationWorkbook.SaveAs(LocationAsURL); } } private bool DoesFileExistInDirectory() { return File.Exists(LocationAsURL); } 

in this case, if the file exists already the save function can just be called on the workbook. However, if the file doesn't exist the SaveAs operation is called on the workbook to save the file to that location. The code for the whole class can be seen here.

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Excel = Microsoft.Office.Interop.Excel; namespace GradeCalculator { public class ExcelWorksheet { private Excel.Application excelApplication; private Excel.Workbook semesterGradeInformationWorkbook; private Excel.Worksheet classInformationWorksheet; public string Name { get; private set; } public string LocationAsURL { get; private set; } public int UsedRows { get { return this.classInformationWorksheet.UsedRange.Rows.Count; } } public int UsedColumns { get { return this.classInformationWorksheet.UsedRange.Columns.Count; } } public ExcelWorksheet(string name) { InitializeExcel(); Name = name; LocationAsURL = Path.Combine(Properties.Settings.Default.DefaultDirectory, Name + ".xlsx"); } private void InitializeExcel() { excelApplication = new Excel.Application(); semesterGradeInformationWorkbook = excelApplication.Workbooks.Add(); classInformationWorksheet = semesterGradeInformationWorkbook.ActiveSheet; excelApplication.Visible = true; } public void Save() { if (DoesFileExistInDirectory()) { semesterGradeInformationWorkbook.Save(); } else { semesterGradeInformationWorkbook.SaveAs(LocationAsURL); } } private bool DoesFileExistInDirectory() { return File.Exists(LocationAsURL); } } } 

Any tips on how to refactor the code so that it is more extensible and I can write a unit test for it? Any other tips on how to improve the class and methods to better implement a better object oriented design would be much appreciated as well.

5

Related questions 1 How to unit test reading an Excel reader? 1 Unit Testing VS 2008 Using Excel 40 How to unit test Excel VBA code Related questions 1 How to unit test reading an Excel reader? 1 Unit Testing VS 2008 Using Excel 40 How to unit test Excel VBA code 2 Mocking an Excel workbook 1 Read Rows in Excel File using c# 4 Unit testing with Excel workbook 1 How to unit test an Excel Workbook project? 0 Unit testing vba from .net - strongly typed COM objects 3 VBA Unit Testing 1 Unit test array from Excel Load 7 more related questions Show fewer related questions

Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like