0

我的代码从文件中获取输入。如果我必须为它编写 NUnit 测试用例,我是否必须更改我的代码并从测试用例而不是文件中获取输入,或者我是否有可能不必更改我的代码。?

请帮忙。

下面给出的是我必须测试的功能之一。

    public void CLEAN_Discipline(string disp)
    {
        //////////////////////////////////////////////////////SQL CONNECTION////////////////////////////////////////////////

        string sqlconnectionString = "server=localhost;" + "database=fastdata;" + "uid=dwh;" + "password=dwh;";
        MySqlConnection cnMySQL = new MySqlConnection(sqlconnectionString);
        MySqlCommand cmdMySQL = cnMySQL.CreateCommand();
        cmdMySQL.CommandText = "Select * from single";
        MySqlDataReader reader;
        String query;
        MySqlCommand cmd2;
        cnMySQL.Open();
        reader = cmdMySQL.ExecuteReader(); 


        while (reader.Read())
        {
            string disp = reader["Discipline"].ToString();

            disp = disp.ToUpper();
            disp = disp.Replace("MS", "");
            disp = disp.Replace("-", "");
            disp = disp.Replace(" ", "");

            string roll = reader["RollNumber"].ToString();


            MySqlConnection cnMySQL2 = new MySqlConnection(sqlconnectionString);
            cnMySQL2.Open();
            query = "UPDATE single SET Discipline = ('" + disp + "')  where RollNumber = '" + roll + "' ";
            cmd2 = new MySqlCommand(query, cnMySQL2);
            cmd2.ExecuteNonQuery();
            cnMySQL2.Close();
        }
        cnMySQL.Close();

        cnMySQL.Open();
        reader = cmdMySQL.ExecuteReader();
        while (reader.Read())
        {
            string roll = reader["RollNumber"].ToString();
            string disp = reader["Discipline"].ToString();
            string degree = reader["Degree"].ToString();
            if (degree == "MS")
            {
                if (roll[0] == 'I')
                {
                    if (disp != "CS" && disp != "SPM" && disp != "")
                    {
                        disp = "UNKNOWN";
                    }
                }
                else if (roll[0] == 'K')
                {
                    if (disp != "CS" && disp != "SPM" && disp != "TC" && disp != "NW")
                    {
                        disp = "UNKNOWN";
                    }
                }
                else if (roll[0] == 'P')
                {
                    if (disp != "CS" && disp != "TC")
                    {
                        disp = "UNKNOWN";
                    }
                }
                else if (roll[0] == 'L')
                {
                    if (disp != "CS" && disp != "SPM" && disp != "TC" && disp != "NW")
                    {
                        disp = "UNKNOWN";
                    }
                }
            }
            if (disp == "UNKNOWN")
            {
                MySqlConnection cnMySQL2 = new MySqlConnection(sqlconnectionString);
                cnMySQL2.Open();
                query = "UPDATE single SET Discipline = ('" + disp + "')  where RollNumber = '" + roll + "' ";
                cmd2 = new MySqlCommand(query, cnMySQL2);
                cmd2.ExecuteNonQuery();
                cnMySQL2.Close();
            }

        }
    }
4

1 回答 1

0

我建议你将你的函数分解成更小的函数,这样你就可以只测试包含逻辑的部分。

在您的情况下,也许您应该提取替换要检查的字符串的各个方面的逻辑,并将其放入一个接受字符串的函数中。然后,您可以在没有所有其他依赖项(数据库读取和写入)的情况下测试该函数。

Michael Feathers 的《有效使用遗留代码》是一个很好的资源,用于解释如何在代码中创建允许测试的接缝。

于 2013-03-23T16:56:04.997 回答