为一堂课进行单元测试? [英] Perform unit test for a class ?

查看:59
本文介绍了为一堂课进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void btnOpen_Click(object sender, EventArgs e)
        // Created OpenFileDialog control using a Forms designer at design-time
        {
            openFileDialog1.ShowDialog();
            //called the ShowDialog method to browse a selected file

            txtfilepath.Text = openFileDialog1.FileName;
            BindDataCSV(txtfilepath.Text);
            //set data into textfilepath
        }
        private void BindDataCSV(string filePath)
        {
            DataTable dt = new DataTable();
            //opened an instance of Datatable

            string[] lines = System.IO.File.ReadAllLines(filePath);
            //ReadAllLines gets a string array from the file

            if (lines.Length > 0)
            {
                //first line to create header

                string firstline = lines[0];
                //reads first line of string array at index 0

                string[] headerLabels = firstline.Split(',');
                //splits the firstline using comma delimited string

                foreach (string headerWord in headerLabels)
                {
                    dt.Columns.Add(new DataColumn(headerWord));
                    //added DataColumns for header
                }

                //for data


                for (int r = 1; r < lines.Length; r++)
                {
                    string[] dataWords = lines[r].Split(',');
                    //split strings into lines
                    DataRow dr = dt.NewRow();
                    //inset a new row into a data table
                    int columnIndex = 0;
                    //start of column is 0 index
                    foreach (string headerWord in headerLabels)
                    {
                        dr[headerWord] = dataWords[columnIndex++];
                        //increment the value by 1 in columnIndex

                    }
                    dt.Rows.Add(dr);
                    //adds DataRow in the DataTable
                }
            }
            if (dt.Rows.Count > 0)
            {
                dataGridView1.DataSource = dt;
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            // Displays a SaveFileDialog so the user can save the Image            
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "|*.csv";
            saveFileDialog1.Title = "Save CSV File";
            saveFileDialog1.ShowDialog();

            // If the file name is not an empty string open it for saving.
            if (saveFileDialog1.FileName != "")
            {
                using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
                {
                    WriteDataTable(dataGridView1.DataSource as DataTable, sw, true);
                }


            }
        }

        // Write data to csv

        public static void WriteDataTable(DataTable sourceTable, TextWriter writer, bool includeHeaders)
        {
            if (includeHeaders)
            {
                List<string> headerValues = new List<string>();
                //opened an instance of List<T> for headerValues
                foreach (DataColumn column in sourceTable.Columns)
                {
                    headerValues.Add(QuoteValue(column.ColumnName));
                }

                writer.WriteLine(String.Join(",", headerValues.ToArray()));
            }

            string[] items = null;
            // if string array is null or empty
            foreach (DataRow row in sourceTable.Rows)
            {
                items = row.ItemArray.Select(o => QuoteValue(o.ToString())).ToArray();
                writer.WriteLine(String.Join(",", items));
                // adds rows to the array and joins comma delimited strings
            }

            writer.Flush();
        }

        private static string QuoteValue(string value)
        {
            return String.Concat("\"", value.Replace("\"", "\"\""), "\"");
        }

    }
}





我的尝试:



嘿伙计们,我正在研究一个项目我被要求为一个班级进行单元测试。我在Vis中编写了一个简单的GUI项目使用C#的工作室。



我已经对单元测试做了一些研究,但我甚至无法弄清楚是否从我编写的代码开始。非常感谢任何正确方向的帮助或指导。



谢谢



What I have tried:

Hey guys, I am working on a project were I have been asked to perform a unit test for a class.I have written a simple GUI project in Visual Studio using C#.

I have done some research on unit testing but am unable to figure out even were to start with the code I have written. Any help or guidance in the right direction would be much appreciated.

Thanks

推荐答案

参见单元测试 - Google搜索 [ ^ ]。


这篇关于为一堂课进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆