gpt4 book ai didi

unit-testing - 带有 SQL 代码的 c# 方法上的 Microsoft Fakes(垫片和/或 stub )

转载 作者:行者123 更新时间:2023-12-04 04:24:09 24 4
gpt4 key购买 nike

我正在尝试使用开箱即用的功能(我相信它是 MSTest.exe)和 Microsoft Fakes( stub 和垫片)来学习更多关于单元测试的知识。

我正在使用 Visual Studio 2012 Ultimate 和 .Net 4.5 Framework。

给定以下调用存储过程 (SQL Server) 的代码,该过程返回单个输出值(为简单起见):

public string GetSomeDatabaseValue()
{
string someValue = String.Empty;

SqlParameter paramater = new SqlParameter();
paramater.ParameterName = "@SomeParameter";
paramater.Direction = ParameterDirection.Output;
paramater.SqlDbType = SqlDbType.NVarChar;
paramater.Size = 50;

try
{
using(SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString))
{
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "SomeStoredProcedure";
command.Parameters.Add(paramater);

connection.Open();
command.ExecuteNonQuery();

if (command.Parameters["@SomeParameter"] != null)
{
someValue= Convert.ToString(command.Parameters["@SomeParameter"].Value);
}
}
}
}
catch(SqlException)
{
throw;
}

return someValue;
}
  1. 能否使用垫片和/或 stub 进行测试,以便将输出值设置为特定值?
  2. 如果是怎么办?
  3. 我是否应该为此使用单元测试?

我关注了this tutorial并设法理解并适应星期几。

作为 MS 员工,我正在等待 VS2012 数据库单元测试功能在 2012 年底可用(或恢复)has commented以便数据库可以被隔离测试。

最佳答案

Microsoft Fakes 不是测试此代码的合适工具。而是创建一个集成测试。在此测试中,使用 SQL 服务器的本地实例,显式创建存储过程期望在数据库中找到的数据,调用存储过程并验证其结果。回滚事务或手动从数据库中删除数据以确保不影响其他测试。

关于unit-testing - 带有 SQL 代码的 c# 方法上的 Microsoft Fakes(垫片和/或 stub ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13419883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com