【问题标题】:Test Driven unit testing with stored procedure in VS 2010VS 2010 中使用存储过程的测试驱动单元测试
【发布时间】:2012-05-01 07:49:37
【问题描述】:

我有一个类 (Not (DAL)),它从 DB 中获取数据并以所需的格式(以特定的类格式)提供给我。

现在我需要编写一个单元测试用例,但 TestContext 数据行总是返回单行。

我想用我的存储过程填充 TestContext。请告诉我如何指定存储过程名称。 提前致谢。

这是代码。

public static List<TextValueField> ExternalDataGet(int providerId)
    {
        return ListFactory<TextValueField>.Create(Keys.QAT, "[stats].[GetExternalDataV2]", new object[] { providerId }, TextValueField.Factory);
    }
public static List<T> Create(string key, string sp, object[] parameters, FactoryDelegate factory)
    {
        List<T> list = new List<T>();
        Database db = DatabaseFactory.CreateDatabase(key);
        string connectionString = db.ConnectionStringWithoutCredentials;

        using (DbCommand cmd = db.GetStoredProcCommand(sp, parameters))
        {
            try
            {
                using (cmd.Connection = db.CreateConnection())
                {
                    cmd.Connection.Open();
                    cmd.CommandTimeout = 0;
                    using (DbDataReader reader = cmd.ExecuteReader())
                    {
                        try
                        {
                            while (reader.Read())
                                list.Add(factory(reader));
                        }
                        finally
                        {
                            if (!reader.IsClosed)   
                                reader.Close();
                        }
                    }
                    cmd.Connection.Close();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw new DataAccessException(ex, key, connectionString, sp, parameters);
            }
            finally
            {
                if (cmd.Connection != null)
                    if (cmd.Connection.State == ConnectionState.Open)
                        cmd.Connection.Close();
            }
        }
        return list;
    }

【问题讨论】:

  • 如何根据提供的参数测试返回 DataSet 的方法。请给我一些想法。
  • 您可能需要发布一些代码来澄清您的问题。
  • [stats].[GetExternalDataV2] 是存储过程的名称
  • 我在等你的回复

标签: unit-testing mstest data-driven-tests


【解决方案1】:

我想用我的存储过程填充 TestContext。请告诉我如何指定存储过程名称。

这应该可以处理您的请求。

public static List<TextValueField> ExternalDataGet(int providerId, string storedProc = "[stats].[GetExternalDataV2]")
{
    return ListFactory<TextValueField>.Create(Keys.QAT, storedProc, new object[] { providerId }, TextValueField.Factory);
}

【讨论】:

  • 我正在等待您的回复。
  • @PavanTiwari:回应什么?
  • 如果你有,你回答上述问题。
  • @PavanTiwari:你还没有问过问题。
  • 我如何用存储过程数据的结果填充我的 TestContext
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
  • 2010-11-10
  • 1970-01-01
  • 2013-07-27
  • 1970-01-01
  • 2010-11-04
相关资源
最近更新 更多