1
如何對下面的類進行單元測試,因爲代碼中沒有任何值,並且沒有返回值,我應該在不修改代碼的情況下執行單元測試。我應該在測試裝置上寫什麼使用帶有void函數的C#進行單元測試
任何幫助?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace ORS
{
public partial class DoctorAddEditMarks : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label2.Text = "" + Session["Username"];
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
Response.Redirect("~/DoctorMainPage.aspx");
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("~/LoginPage.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
string coursecode = DropDownList1.SelectedValue.ToString();
string marktype = DropDownList2.SelectedValue.ToString();
string fullmark = TextBox1.Text;
string stid = row.Cells[0].Text;
TextBox txtmark = (TextBox)row.Cells[1].FindControl("TextBox2");
string mark = txtmark.Text;
String strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
String query = "insert into Marks values (@St_ID, @CourseCode, @MarkType, @Mark, @FullMark)";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@CourseCode", coursecode);
cmd.Parameters.AddWithValue("@St_ID", stid);
cmd.Parameters.AddWithValue("@MarkType", marktype);
cmd.Parameters.AddWithValue("@Mark", mark);
cmd.Parameters.AddWithValue("@FullMark", fullmark);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Marks are SuccessFully Saved');window.location ='DoctorAddEditMarks.aspx';", true);
}
}
}
您在GUI事件處理程序中擁有業務邏輯/數據庫訪問代碼。這不適合單元測試。通常你會把它分成不同的層次,或者至少在點擊處理程序的代碼中做一個可測試的(例如bool-returning)函數。或者您正在使用可編寫腳本的GUI模擬器進行測試,並測試客戶端ASP頁面結果(編碼的UI測試) – dlatikay
或編寫一個額外的測試方法來檢查點擊處理程序應該插入的數據。 – dlatikay
我不知道如何做點擊處理程序 –