Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6332957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:22:20+00:00 2026-05-24T18:22:20+00:00

I have an object that reads data from an Excel file using, which takes

  • 0

I have an object that reads data from an Excel file using, which takes a IDbConnection, IDbDataAdapter and an IDbCommand. I use the adapters fill method to populate a table with data, and this is how I am currently mocking it:

[TestCase]
public void TestReadCellsFromSpreadsheetReadsSuccessfully()
{
    var cells = new List<ReportData>
                    {
                        new ReportData { CellId = 1, ExcelCellLocation = "A1"},
                        new ReportData { CellId = 2, ExcelCellLocation = "A2"},
                        new ReportData { CellId = 3, ExcelCellLocation = "A3"},
                        new ReportData { CellId = 4, ExcelCellLocation = "A4"}
                    };

    _mockAdapter.Setup(a => a.Fill(It.IsAny<DataSet>()))
        .Callback((DataSet ds) =>
                      {
                          if (ds.Tables["Table"] == null)
                          {
                              ds.Tables.Add("Table");
                              ds.Tables["Table"].Columns.Add(new DataColumn());
                          }

                          var row = ds.Tables["Table"].NewRow();
                          row[0] = "Test";

                          ds.Tables["Table"].Rows.Add(row);
                      });

    var excelReader = new ExcelReader(_mockConnection.Object, _mockAdapter.Object, _mockCommand.Object);
    excelReader.ReadCellsFromSpreadsheet("Deal Summary", cells);

    _mockCommand.VerifySet(c => c.CommandText = It.IsAny<string>(), Times.Exactly(cells.Count));
    _mockAdapter.VerifySet(a => a.SelectCommand = _mockCommand.Object, Times.Exactly(cells.Count));
    _mockAdapter.Verify(a => a.Fill(It.IsAny<DataSet>()), Times.Exactly(cells.Count));
}

This implementation works, but I feel like I’m doing too much to Mock the adapter… is there a better way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:22:21+00:00Added an answer on May 24, 2026 at 6:22 pm

    Do not pass those 3 objects as parameters. Instead pass IDataReader, IDataProvider or sth like that that returns data. Then You just mock this object. And you don’t need reference to System.Data in project containing ExcellReader.

    And two other things I don’t like about your code.
    Why TestCase instead of Test?

    Are you sure you want to create command and fill dataset for each column separately? (but maybe I don’t understand your code)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple problem that reads an Excel file (using interop) and fills
I have a SSIS package which reads an Excel File (Data Flow Source) and
I have a SL application that reads some data from the database, which I
Ok - I have a WCF Service which reads an excel file from a
I am trying to read data from excel file in asp.net. I have added
I have written a program that reads in a File object (really an XML
I have wrote a code that can read an excel 2007 file using Microsoft
i have a java process that reads data from a socket server. Thus i
I have an SSIS 2008 package that reads data from an Access database (OLEDB
I have an object that needs a test if the object data is valid.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.