title: Create Moq for Epplus Excel file
body: <p>My first question here. I have looked my query but could not find a helpful answer. </p>
  
  <p>My task is to write unit test case for my excel file. The issue I am facing is that we using Epplus for excel files and I am not sure how can we write unit test cases for this. I looked up and found that we can also use MOQ to mock up. But again I could not find any helpful links for mocking an excel file that uses Epplus. I found this link <a href="https://stackoverflow.com/questions/24703971/unit-testing-classes-that-use-epplus">Unit testing classes that use EPPlus</a> but I am not sure how I can implement this .</p>
  
  <p>I would appreciate if someone can provide a sample of how to write a simple unit test for the excel file. The test can be to check if file uploaded is an excel file or not, checking if the excel is empty or not etc.</p>
  
  <p>Sorry at this moment I dont have any sample. What I can share is the code where I am reading the excel file:</p>
  
  <pre><code>public class MyController : Controller
  {
    [HttpPost("upload")]
    public async Task&lt;IActionResult&gt; UploadFile(IFormFile file)
    {
     JArray data = new JArray();
      using (ExcelPackage package = new ExcelPackage(file.OpenReadStream()))
      {
        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
        //Check if excel is empty.
        if (worksheet.Dimension == null)
        {
           return BadRequest("File is blank.");
        }
       data = Helper.CreateJson(worksheet);
                  }
       return Ok(data);
    }
  }
  </code></pre>
  
  <p>I had created a helper class as:</p>
  
  <pre><code>public static JArray CreateJson(ExcelWorksheet worksheet)
  {
    JArray data = new JArray();
    JObject jobject = new JObject();
  
    int rowCount = worksheet.Dimension.End.Row;
    int colCount = worksheet.Dimension.End.Column;
  
      for (int row = 1; row &lt;= rowCount; row++)
      {
         for (int col = 1; col &lt;= colCount; col++)
          {
            var value = worksheet.Cells[row, col].Value;
            //Excel has 2 columns and I want to create a json from that.
            if (col == 1)              
            {
               jObject.Add("ID", rowValue.ToString());
            }
            else
            {
               jObject.Add("Name", rowValue.ToString());
            }
          }
           data.Add(jObject);
           jObject= new JObject();
       }
     return data;
  }
  </code></pre>
  
  <p>This is the Test Class i have so far.</p>
  
  <pre><code>public class TestClass
  {
      private MyController _controller;
      public TestClass()
      {
        _controller = new MyController (); 
      }
  
        [Fact]
      public void Upload_WhenCalled()
      {
          //var file = new FileInfo(@"C:\myfile.xlsx");
          //...what next?
  
          var file = new Mock&lt;IFormFile&gt;();
          var content = File.OpenRead(@"C:\myfile.xlsx");
  
          var result = _controller.UploadFile(file.Object);
          //When I debug it throws error "Object reference not set to an instance of an object."
      }
  }
  </code></pre>