ConnectionString属性单元测试业务逻辑函数

时间:2018-03-30 06:45:50

标签: c# unit-testing ado.net connection-string n-tier-architecture

我正在尝试对3层Windows窗体C#应用程序的业务逻辑功能执行单元测试。但是我收到了这个错误:

  

消息:测试方法Cognizant.Dotnet.EMS.UnitTest.BusinessAddEmpDtlsTest.TestBusinessAddEmpDtls15抛出异常:       System.InvalidOperationException:尚未初始化ConnectionString属性。

现在我是单元测试的新手。我在我的解决方案中创建了一个visual studio单元测试项目。这是我的业务层或层类:

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; }        
}

数据层类是:

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;
    }       
}

单元测试课程如下:

[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成功将数据插入到数据库中。除了单元测试功能外,一切正常。

有人能告诉我这里的问题是什么,为什么会发生异常?我已经在数据层类构造函数中初始化了连接字符串。

1 个答案:

答案 0 :(得分:1)

连接字符串似乎来自配置管理器

conStr = ConfigurationManager.AppSettings["Connection"];

确认在运行单元测试时获得了正确的连接字符串。

如果没有,请检查以确保单元测试项目具有 app.config 文件,其中包含进行测试所需的设置。

也就是说,考虑一下当前有关代码的设计,因为它似乎与实施问题紧密相关,这使得难以单独测试而没有负面的副作用。