1

我刚刚开始使用 Moles 来模拟一些棘手的遗留代码。本质上,我正在尝试让 SqlDataAdapter 与 Moles 一起使用。(顺便说一句,我在 SqlDataReader 和 SqlCommand 类中成功使用了 moles。)我试图在下面创建一个“简单”单元测试示例,我试图让 SqlDataAdaptor “填充”提供的 DataSet。然后在使用 Moles 时,我在模拟从数据集中检索数据的各种调用。我相信我已经正确设置了 DataSet,以便数据检索将返回预期的“moled”对象并做正确的事情。

当我运行下面的代码时,我可以看到 FillDataSetString lambda 表达式正在执行,并且“d”被设置为“moled”ds。但是当Fill方法返回时,传入的DataSet(“dset”)仍然是一个常规的“DataSet”,而不是“moled DataSet”。因此,第一个 Assert 无法正确运行并引发 IndexOutOfRangeException(“找不到表 0。”)。在第一个断言中,我期望在评估 dset.Tables[0].Rows.Count 时调用以下“moled”方法:

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

但由于 dset 不是“被模制的”数据集,因此这些调用都不会发生。任何帮助弄清楚 Moles 对 SqlDataAdapter 的数据集参数所做的事情将不胜感激。

要使以下工作,您必须安装“Moles”,引用 System.Data、System.Xml,创建“System.Data.moles”引用。我正在使用 Moles 框架的 0.94.0.0 并在 VS.NET 2010 中运行它,测试项目的“目标框架”设置为“.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");
    }
}
4

1 回答 1

0

根据@StingyJack 提出的模拟 Fill 方法的建议,几个月后重新审视这个问题,我想出了以下内容来支持我的模拟要求。它仍然没有真正回答为什么数据集没有被我的摩尔数据集替换。

    [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");
        }
    }
于 2011-09-27T22:06:02.613 回答