ConnectionString 属性 单元测试业务逻辑函数时发生未初始化错误
ConnectionString Property Not initialized error occurded while unit testing business logic function
我正在尝试对 3 层 Windows 表单 C# 应用程序的业务逻辑功能执行单元测试。但我收到此错误:
Message: Test method Cognizant.Dotnet.EMS.UnitTest.BusinessAddEmpDtlsTest.TestBusinessAddEmpDtls15 threw exception:
System.InvalidOperationException: The ConnectionString property has not been initialized.
现在我是单元测试的新手。我在我的解决方案中创建了一个 visual studio 单元测试项目。这是我的业务层或层 class:
public class BusinessAddEmployee {
DataAddEmployee objDataAddEmp;
public BusinessAddEmployee()
{
objDataAddEmp = new DataAddEmployee();
}
public int BusinessAddEmpDetails(EntityAddEmployee objEntityAddEmployee) {
SqlParameter[] objDataParams = new SqlParameter[5];
objDataParams[0] = new SqlParameter("@EmpId", SqlDbType.Int) {Value = objEntityAddEmployee.EmpID};
objDataParams[1] =
new SqlParameter("@EmpName", SqlDbType.VarChar, 25) {Value = objEntityAddEmployee.EmpName};
objDataParams[2] =
new SqlParameter("@DeptName", SqlDbType.VarChar, 25) {Value = objEntityAddEmployee.DepartmentName};
objDataParams[3] =
new SqlParameter("@Location", SqlDbType.VarChar, 25) {Value = objEntityAddEmployee.Location};
objDataParams[4] =
new SqlParameter("@ContactNumber", SqlDbType.BigInt) {Value = objEntityAddEmployee.ContactNo};
var result = objDataAddEmp.DataAddEmployeeDetails(objDataParams);
return result;
}
}
这是 AddEmployee
的实体 layer/tier class
public class EntityAddEmployee {
public Int32? EmpID { get; set; }
public string EmpName { get; set; }
public string DepartmentName { get; set; }
public string Location { get; set; }
public long? ContactNo { get; set; }
}
数据层class是这样的:
public class DataAddEmployee
{
private static string conStr;
private SqlConnection objConnnection;
SqlCommand objCommand;
public DataAddEmployee()
{
conStr = ConfigurationManager.AppSettings["Connection"];
objConnnection = new SqlConnection(conStr);
}
public int DataAddEmployeeDetails(SqlParameter[] objParams) {
objCommand = new SqlCommand("USPEmpDtls", objConnnection);
objCommand.CommandType = CommandType.StoredProcedure;
objCommand.Parameters.AddRange(objParams);
objConnnection.Open();**//This line throws the exception**
int result;
try
{
result = objCommand.ExecuteNonQuery();
}
catch (Exception e)
{
throw new InvalidOperationException();
}
objConnnection.Close();
return result;
}
}
单元测试class如下:
[TestClass]
public class BusinessAddEmpDtlsTest
{
private BusinessAddEmployee objBusinessAddEmp;
private EntityAddEmployee objEntityAddEmp;
[TestInitialize]
public void Init()
{
objBusinessAddEmp = new BusinessAddEmployee();
objEntityAddEmp = new EntityAddEmployee();
}
[TestMethod]
public void TestBusinessAddEmpDtls15()
{
objEntityAddEmp.EmpID = 11112;
objEntityAddEmp.EmpName = "John Die";
objEntityAddEmp.DepartmentName = "Audit";
objEntityAddEmp.Location = "Dhaka";
objEntityAddEmp.ContactNo = Convert.ToInt64(01999999999);
Assert.AreEqual(1, objBusinessAddEmp.BusinessAddEmpDetails(objEntityAddEmp));//**I have compared with 1 since it is a valid operation**//
}
}
连接字符串工作正常,因为我已经成功地将数据从我创建的 WindowsForm
插入到数据库中。除了单元测试功能外,一切正常。
谁能告诉我这里的问题是什么,为什么会出现异常?我已经在数据层 class 构造函数中初始化了连接字符串。
连接字符串似乎来自配置管理器
conStr = ConfigurationManager.AppSettings["Connection"];
确认您在 运行 单元测试时获得正确的连接字符串。
如果没有,则检查以确保单元测试项目有一个 app.config 文件,其中包含进行测试所需的设置。
也就是说,请考虑审查相关代码的当前设计,因为它似乎与实现问题紧密相关,这使得很难在没有负面影响的情况下进行孤立测试 side-effects。
我正在尝试对 3 层 Windows 表单 C# 应用程序的业务逻辑功能执行单元测试。但我收到此错误:
Message: Test method Cognizant.Dotnet.EMS.UnitTest.BusinessAddEmpDtlsTest.TestBusinessAddEmpDtls15 threw exception: System.InvalidOperationException: The ConnectionString property has not been initialized.
现在我是单元测试的新手。我在我的解决方案中创建了一个 visual studio 单元测试项目。这是我的业务层或层 class:
public class BusinessAddEmployee {
DataAddEmployee objDataAddEmp;
public BusinessAddEmployee()
{
objDataAddEmp = new DataAddEmployee();
}
public int BusinessAddEmpDetails(EntityAddEmployee objEntityAddEmployee) {
SqlParameter[] objDataParams = new SqlParameter[5];
objDataParams[0] = new SqlParameter("@EmpId", SqlDbType.Int) {Value = objEntityAddEmployee.EmpID};
objDataParams[1] =
new SqlParameter("@EmpName", SqlDbType.VarChar, 25) {Value = objEntityAddEmployee.EmpName};
objDataParams[2] =
new SqlParameter("@DeptName", SqlDbType.VarChar, 25) {Value = objEntityAddEmployee.DepartmentName};
objDataParams[3] =
new SqlParameter("@Location", SqlDbType.VarChar, 25) {Value = objEntityAddEmployee.Location};
objDataParams[4] =
new SqlParameter("@ContactNumber", SqlDbType.BigInt) {Value = objEntityAddEmployee.ContactNo};
var result = objDataAddEmp.DataAddEmployeeDetails(objDataParams);
return result;
}
}
这是 AddEmployee
public class EntityAddEmployee {
public Int32? EmpID { get; set; }
public string EmpName { get; set; }
public string DepartmentName { get; set; }
public string Location { get; set; }
public long? ContactNo { get; set; }
}
数据层class是这样的:
public class DataAddEmployee
{
private static string conStr;
private SqlConnection objConnnection;
SqlCommand objCommand;
public DataAddEmployee()
{
conStr = ConfigurationManager.AppSettings["Connection"];
objConnnection = new SqlConnection(conStr);
}
public int DataAddEmployeeDetails(SqlParameter[] objParams) {
objCommand = new SqlCommand("USPEmpDtls", objConnnection);
objCommand.CommandType = CommandType.StoredProcedure;
objCommand.Parameters.AddRange(objParams);
objConnnection.Open();**//This line throws the exception**
int result;
try
{
result = objCommand.ExecuteNonQuery();
}
catch (Exception e)
{
throw new InvalidOperationException();
}
objConnnection.Close();
return result;
}
}
单元测试class如下:
[TestClass]
public class BusinessAddEmpDtlsTest
{
private BusinessAddEmployee objBusinessAddEmp;
private EntityAddEmployee objEntityAddEmp;
[TestInitialize]
public void Init()
{
objBusinessAddEmp = new BusinessAddEmployee();
objEntityAddEmp = new EntityAddEmployee();
}
[TestMethod]
public void TestBusinessAddEmpDtls15()
{
objEntityAddEmp.EmpID = 11112;
objEntityAddEmp.EmpName = "John Die";
objEntityAddEmp.DepartmentName = "Audit";
objEntityAddEmp.Location = "Dhaka";
objEntityAddEmp.ContactNo = Convert.ToInt64(01999999999);
Assert.AreEqual(1, objBusinessAddEmp.BusinessAddEmpDetails(objEntityAddEmp));//**I have compared with 1 since it is a valid operation**//
}
}
连接字符串工作正常,因为我已经成功地将数据从我创建的 WindowsForm
插入到数据库中。除了单元测试功能外,一切正常。
谁能告诉我这里的问题是什么,为什么会出现异常?我已经在数据层 class 构造函数中初始化了连接字符串。
连接字符串似乎来自配置管理器
conStr = ConfigurationManager.AppSettings["Connection"];
确认您在 运行 单元测试时获得正确的连接字符串。
如果没有,则检查以确保单元测试项目有一个 app.config 文件,其中包含进行测试所需的设置。
也就是说,请考虑审查相关代码的当前设计,因为它似乎与实现问题紧密相关,这使得很难在没有负面影响的情况下进行孤立测试 side-effects。