简体   繁体   中英

Unit Testing using C# with void function

How can I do unit testing for the below class, as there is no any values in the code and there is no return and I should do the unit testing without modifying the code. What should I write in the test unit

Any Help?

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

        }
}
}

The first step to being able to test this would be to isolate the database code into its own class. It's pretty much impossible to unit test an .aspx page.

The test you write is technically not a unit test - it's an integration test. The difference is just that it's testing more than just your code. It's testing the combined functionality of your code and your database.

This page shows some simple ways to write those tests. It follows the typical arrange/act/assert pattern.

Arrange

  • Make sure there's no data in the table that will interfere with what I'm about to test
  • Prepare my input

Act

  • Call the method to insert my data

Assert

  • Verify that the data is in the table just as I expect

Then I typically do a cleanup at the end to delete whatever test data I modified, which is often just repeating the first step.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM