4

I am using xUnit and Moq for writing unit tests.

My ExcelData class has below code

       public DataTable ReadFromExcel(CloudBlockBlob MasterDataSourceBlob, string SheetName)
        {
            DataTable dataInExcelSheet = new DataTable();
            using (var memoryStream = new MemoryStream())
            {
                MasterDataSourceBlob.DownloadToStreamAsync(memoryStream);
                var headers = new List<string>();
               IExcelDataReader excelReader = ExcelReaderFactory.CreateReader(memoryStream);
          }

Inorder to mock ExcelReaderFactory.CreateReader() , I created new class and Interface as below and returned IExcelDataReader .

ExcelDataMock.cs file has below code

   public  class ExcelDataMock
    {

        public IExcelDataReader CreateReader(MemoryStream memoryStream)
        {
           return ExcelReaderFactory.CreateReader(memoryStream); 
        }
    }

IExcelDataMock.cs file has below code

   public interface IExcelDataMock
    {
        IExcelDataReader CreateReader(MemoryStream memoryStream);
    }

My Test class has below code

      [Fact]
        public void ReadFromExcel_Success()
        {
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);
            writer.Write("sample data");
            writer.Flush();
            stream.Position = 0;

            var mockExcelDataMock = new Mock<IExcelDataMock>();
            mockExcelDataMock.Setup(data => data.CreateReader(It.IsAny<MemoryStream>())).Returns<IExcelDataReader>(
                excel =>
            {

                var item = new Mock<IExcelDataReader>();

                return item.Object;
            }
            );


            var mockCloudBlockBlob = new Mock<CloudBlockBlob>(new Uri("https://samplestorage.blob.core.windows.net/samplecontainer"));


            mockCloudBlockBlob.Setup(blob => blob.DownloadToStreamAsync(It.IsAny<Stream>()))
                .Callback((Stream target) => stream.CopyTo(target)) 
                .Returns(Task.CompletedTask);


            string SheetName = null;

            this._iExcelOperations = new ExcelData(mockExcelDataMock.Object);

            this._iExcelOperations.ReadFromExcel(mockCloudBlockBlob.Object, SheetName);

        }

enter image description here

If I use ExcelReaderFactory.CreateReader(memoryStream) I got below error

No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.'

If I use this._iExcelOperationsMock.CreateReader(memoryStream) I got error as below

System.ArgumentException: 'Object of type 'System.IO.MemoryStream' cannot be converted to type 'ExcelDataReader.IExcelDataReader'.'

enter image description here

How to resolve this issue.

Update 1:

Error:

invalid file signature

enter image description here

chandra sekhar
  • 1,093
  • 4
  • 14
  • 27
  • We are going to need a better view of the code you are showing. the current snippets are too disjointed to get a full understanding of what is being asked. – Nkosi Jul 19 '19 at 11:48
  • Actually I am unable to mock ExcelReaderFactory.CreateReader(memoryStream) . It is throwing given error. Kindly suggest resolution. – chandra sekhar Jul 19 '19 at 12:18
  • The sample code you have provided is not complete so it would make it difficult to provide a proper solution with incomplete information. Provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) that clarifies your specific problem – Nkosi Jul 19 '19 at 12:48
  • I also observed that you are calling `MasterDataSourceBlob.DownloadToStreamAsync(memoryStream);` in a synchronous method, which will cause problems – Nkosi Jul 19 '19 at 12:49
  • Thanks for your suggestion. Issue has been resolved after adding System.Text.Encoding.CodePages as per https://stackoverflow.com/questions/49215791/vs-code-c-sharp-system-notsupportedexception-no-data-is-available-for-encodin .Now I am getting different Issue which I have specified in Update 1 in question. – chandra sekhar Jul 19 '19 at 13:10
  • @chandrasekhar Do you still have the issue mentioned in your update. – Naveen Jan 08 '20 at 16:54

0 Answers0