开发者

Why isn't Moles returning "moled" DataSet when mocking SqlDataAdaptor?

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 SqlD开发者_如何学运维ataAdapter'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");
    }
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜