lundi 2 mars 2015

C# unit tests on big methods

I am currently working on a university assignment and have quite foolishly bundled all of my code into two hugs button methods, the first one that reads in a text file and displays the data in a data grid view, and the second one is this:



private void btnCalc_Click(object sender, EventArgs e)
{
//Sets variables needed for calculations
double sum1 = 0;
int sum2 = 0;
int sum3 = 0;
int sum4 = 0;
int sum5 = 0;
int sum6 = 0;
string heart = string.Empty;

for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
//Gets the total of all the rows in each column and stores as a variable
sum1 += Convert.ToDouble(dataGridView1.Rows[i].Cells["Speed (kph)"].Value);
sum2 += Convert.ToInt32(dataGridView1.Rows[i].Cells["Heart Rate"].Value);
sum3 += Convert.ToInt32(dataGridView1.Rows[i].Cells["Cadence"].Value);
sum4 += Convert.ToInt32(dataGridView1.Rows[i].Cells["Altitude"].Value);
sum5 += Convert.ToInt32(dataGridView1.Rows[i].Cells["Power"].Value);
sum6 += Convert.ToInt32(dataGridView1.Rows[i].Cells["PowerBalance"].Value);

//counts the amount rows in the datagrid
int count_row = dataGridView1.Rows.Count;

//gets an average by dividing the total of all rows by the amount of rows
double avg = sum1 / count_row;
double avg2 = sum2 / count_row;
double avg3 = sum5 / count_row;
double avg4 = sum4 / count_row;
//rounds avg to a single decimal point
avg = Math.Round(avg, 1);

//Selects the maximum value from each column and converts to a double
var MSpeed = dataGridView1.Rows.Cast<DataGridViewRow>().Max(r => Convert.ToDouble(r.Cells["Speed (kph)"].Value));
var MHeart = dataGridView1.Rows.Cast<DataGridViewRow>().Max(r => Convert.ToInt32(r.Cells["Heart Rate"].Value));
var MPower = dataGridView1.Rows.Cast<DataGridViewRow>().Max(r => Convert.ToInt32(r.Cells["Power"].Value));
var MAltitude = dataGridView1.Rows.Cast<DataGridViewRow>().Max(r => Convert.ToInt32(r.Cells["Altitude"].Value));
//Calculates distance by dividing the total amount of speed by the amount of seconds in an hour
var Distance = sum1 / 3600;
//Rounds the distance value to a single decimal point
Distance = Math.Round(Distance, 1);

var Minhr = dataGridView1.Rows.Cast<DataGridViewRow>().Max(r => Convert.ToInt32(r.Cells["Heart Rate"].Value));
foreach (DataGridViewRow Rows in dataGridView1.Rows)
{
//selects minimum heart rate from a value above '0'
heart = (string)Rows.Cells["Heart Rate"].Value;
if (Convert.ToInt32(heart) < Minhr && Convert.ToInt32(heart) != 0)
{
Minhr = Convert.ToInt32(heart);
}
}

//Add values to rich text box
richTextBox1.Text = "Distance Covered:" + Distance.ToString() + System.Environment.NewLine
+ "Max Speed:" + MSpeed.ToString() + System.Environment.NewLine
+ "Average Speed:" + avg.ToString() + System.Environment.NewLine
+ "Maximum Heart Rate:" + MHeart.ToString() + System.Environment.NewLine
+ "Minimum Heart Rate:" + Minhr.ToString() + System.Environment.NewLine
+ "Average Heart Rate:" + avg2.ToString() + System.Environment.NewLine
+ "Maximum Power:" + MPower.ToString() + System.Environment.NewLine
+ "Average Power:" + avg3.ToString() + System.Environment.NewLine
+ "Maximum Altitude:" + MAltitude.ToString() + System.Environment.NewLine
+ "Average Altitude:" + avg4.ToString();

}
}


I have looked at some tutorials but cannot figure out how I would go about doing unit testing on a method like this, any help would be appreciated.


Aucun commentaire:

Enregistrer un commentaire