如何对Data acces层代码进行Nunit测试?

时间:2019-01-01 11:42:32

标签: c# asp.net visual-studio nunit nunit-console

我是单元测试的新手。我已经开始使用Nunit编写单元测试代码。我想知道对Ado.net代码进行单元测试的正确方法,该代码从数据库中获取数据。

这是测试代码的正确方法吗?在测试方法中,我将发送所需的数据。由于使用了Transaction,它将回滚,所以我已经使用了它。

[Test]
public void YourTest() 
{
    using (TransactionScope scope = new TransactionScope())
    {
    // test code of the below method
    }
}

访问sql服务器数据的代码。

public void method(int Id) 
{
    using (SqlConnection con = new SqlConnection(sConstr))
    {
        SqlCommand cmd = new SqlCommand("getConfiguration", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Id",Id);
        OpenConnection(con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
        }
}

0 个答案:

没有答案