以下の従業員モデルクラスとコンソールクライアントがあります。.NETのデータ挿入シナリオでNunitテストケースを行うためのガイダンスが必要
Employeeクラス: -
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public int phoneNumber { get; set; }
public Employee()
{
}
public Employee(string fname,string lname,int age,int phone)
{
this.FirstName = fname;
this.LastName = lname;
this.Age = age;
this.phoneNumber = phone;
}
public void InsertEmployee()
{
SqlConnection con = new SqlConnection("sqlconnection");
SqlCommand cmd = new SqlCommand("sp_insert", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("fname", this.FirstName);
cmd.Parameters.AddWithValue("lname", this.LastName);
cmd.Parameters.AddWithValue("age", this.Age);
cmd.Parameters.AddWithValue("phoneno",this.phoneNumber);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public List<Employee> GetAllEmployees()
{
SqlConnection connection = new SqlConnection("sqlconnection");
SqlCommand cmd = new SqlCommand("GetAllEmployees", connection);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
List<Employee> employeeList = new List<Employee>();
while (dr.Read())
{
Employee emp = new Employee();
emp.EmployeeId = int.Parse(dr["empID"].ToString());
emp.FirstName = dr["fname"].ToString();
emp.LastName = dr["lname"].ToString();
emp.Age= int.Parse(dr["age"].ToString());
emp.phoneNumber= int.Parse(dr["phone"].ToString());
employeeList.Add(emp);
}
return employeeList;
}
}
******Client code****
class Program
{
static void Main(string[] args)
{
Employee newEmp = new Employee("Ram", "Prem", 30, 90000007);
newEmp.InsertEmployee();
List<Employee> empList = newEmp.GetAllEmployees();
}
}
********************
上記のコードは動作し、それは結構です。
今、私はInsertメソッドとFetchメソッドのNunitテストメソッドを書くように言われました。
はどのようにして、以下の条件で挿入するためにNUnitのテストメソッドを書くことができます: - マニュアルvalidation.Itは、NUnitのテストの一部であるべきではありませんdatabase.Thereに挿入されてしまった供給いままでの値を確保する方法
1)。
2)新しい列がテーブルに導入された場合。
EmployeeモデルでCityプロパティが追加され、Cityパラメータがパラメータとして渡されました。
新しい列のCity Nullable列がテーブルに追加され、Insertストアドプロシージャでは、開発者がinsert文に新しい列を追加しましたが、Cityパラメータがプロシージャに追加されました。 NUnitのテストは、それは市がテーブルに挿入されていないです(このバグを特定する方法?上記の条件でテストするためにNUnitのテストメソッドを記述するために
どのようにこの上記のシナリオでは
?
このコードの設計はテストが難しく、プロジェクトが成熟するにつれ悪くなるだけです。 SOLID原則をレビューし、このコードをリファクタリングします。 – Nkosi