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 6627015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:57:46+00:00 2026-05-25T21:57:46+00:00

I’ve just started to use Moles to mock some tricky legacy code. In essence,

  • 0

I’ve just started to use Moles to mock some tricky legacy code. In essence, I’m trying get a SqlDataAdapter to work with Moles. (BTW, I’ve been successful using moles with the SqlDataReader and SqlCommand classes.) I’ve tried to create a “simple” unit test example below where I’m trying to get SqlDataAdaptor to “fill” the provided DataSet. Then when using Moles, I’m mocking the various calls in retrieving data from the data set. I believe I have set the DataSet up correctly so that retrieval of data will return the expected “moled” object and do the right thing.

When I run the below I can see that FillDataSetString lambda expression is being executed and “d” is being set to the “moled” ds. But when the Fill method returns, the DataSet passed in (“dset”) is still a regular “DataSet” and not the “moled DataSet”. Thus the first Assert doesn’t operate correctly and throws an IndexOutOfRangeException (“Cannot find table 0.”). In the first Assert, I’m expecting the following “moled” methods to be called when dset.Tables[0].Rows.Count is evaluated:

    ds.TablesGet
    tables.ItemGetInt32
    table.RowsGet
    rows.CountGet

But since dset is not not the “moled” DataSet, none of those calls happen. Any help figuring out what Moles is doing with SqlDataAdapter’s dataset parameter would be much appreciated.

To get the below to work, you must install “Moles”, reference System.Data, System.Xml, create a “System.Data.moles” reference. I’m using 0.94.0.0 of the Moles framework and running this in VS.NET 2010, with the test project’s “Target Framework” set as “.NET Framework 4.0”.

using System.Data;
using System.Data.Moles;
using System.Data.Common.Moles;
using System.Data.SqlClient;
using System.Data.SqlClient.Moles;
using System.Xml.Serialization;

[TestClass]
public class UnitTest1
{

    [TestMethod]
    [HostType("Moles")]
    public void IsolatedSqlDataAdaptorTest()
    {
        // Arrange
        Dictionary<string, object> backing = new Dictionary<string, object>() 
        {
            {"field", 5},
        };

        MSqlConnection.AllInstances.Open = (c) => { };
        MSqlConnection.AllInstances.Close = (c) => { };
        MSqlDataAdapter.ConstructorStringSqlConnection =
        (@this, cmd, conn) =>
        {
            // Setup a moled DataSet with 1 Table and 1 Row
            MDataRow row = new MDataRow()
            {
                // This is the method that ultimately gets called.
                ItemGetString = (key) => { return backing[key]; },
            };

            MDataRowCollection rows = new MDataRowCollection();
            rows.CountGet = () => { return 1; };
            rows.ItemGetInt32 = (i) => { return row; };

            MDataTable table = new MDataTable();
            table.RowsGet = () => { return rows; };

            MDataTableCollection tables = new MDataTableCollection();
            tables.ItemGetInt32 = (i) => { return table; };

            MDataSet ds = new MDataSet();
            ds.TablesGet = () => { return tables; };

            MSqlDataAdapter sdaMole = new MSqlDataAdapter(@this);
            MDbDataAdapter ddaMole = new MDbDataAdapter(sdaMole)
            {
                FillDataSetString = (d, s) =>
                {
                    d = ds;
                    return 1;
                },
            };
        };

        // Act
        DataSet dset = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(
            "select something from aTable",
            new SqlConnection());
        da.Fill(dset, "aTable");

        // Assert
        Assert.AreEqual(1, dset.Tables[0].Rows.Count, "Count");
        Assert.AreEqual(5, dset.Tables[0].Rows[0]["field"], "field");
    }
}
  • 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-25T21:57:47+00:00Added an answer on May 25, 2026 at 9:57 pm

    Taking a fresh look at this question after a number of months as prompted by a suggestion, by @StingyJack, to mock the Fill method, I came up with the following to support my mocking requirements. It still doesn’t really answer why the dataset was not being replaced with my moled dataset.

        [TestMethod]
        [HostType("Moles")]
        public void IsolatedSqlDataAdaptorTestWithFill()
        {
            // Arrange
            MSqlConnection.AllInstances.Open = c => { };
            MSqlConnection.AllInstances.Close = c => { };
            MSqlDataAdapter.ConstructorStringSqlConnection = (@this, cmd, conn) => { };
            MDbDataAdapter.AllInstances.FillDataSetString = (da, ds, s) =>
                {
                    var dt = new DataTable(s);
                    dt.Columns.Add(new DataColumn("string", typeof(string)));
                    dt.Columns.Add(new DataColumn("int", typeof(int)));
                    dt.Rows.Add("field", 5);
                    ds.Tables.Add(dt);
                    return 1;
                };
    
            // Act
            using (var dset = new DataSet())
            {
                using (var conn = new SqlConnection())
                {
                    using (var da = new SqlDataAdapter("select something from aTable", conn))
                    {
                        da.Fill(dset, "aTable");
                    }
                }
    
                // Assert
                Assert.AreEqual(1, dset.Tables[0].Rows.Count, "Count");
                Assert.AreEqual("field", dset.Tables[0].Rows[0]["string"], "string");
                Assert.AreEqual(5, dset.Tables[0].Rows[0]["int"], "int");
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.