单元测试列表<;struct>;

本文关键字:struct gt lt 列表 单元测试 | 更新日期: 2023-09-27 18:00:30

作为单元测试和测试驱动开发的新手,我在单元测试刚刚为我正在进行的项目编写的新函数时遇到了困难。我需要解析网页中的数据,并将关键数据存储在数据库中以备稍后存储。我创建了一个结构来保存我需要的单个记录,并创建了该结构的列表来保存所有收集的数据,然后在解析数据时返回所述列表。一切如预期,但单元测试失败??我不明白为什么。。。请帮忙!!

这是结构和函数:

//===================================================================================================================
    /// lsBuildInstalData
    /// <summary> 
    ///                    This function will parse out the required data from the input list and store it in a private struct.
    ///                    Year (allowing up to 10) : Instalment Amount (pick up the first 4 and skip the rest) : Values (3 values; Year/InstalAmt/Due)
    /// </summary>
    /// 
    /// Parameters
    /// <param name="sTaxDataLine">   Data to parse the values out of.</param>
    /// 
    /// <returns>          Struct List<> of the parsed values.</returns>
    ///
    /// DataBase Use       N/A
    ///                                    
    /// Change Log         04/23/14   MS   Initial development.
    ///                                    
    //===================================================================================================================
    public struct InstalData
    {
        // -- Set the properties... 
        private string sDueAmt, sInstalAmt, sYear;
        public string DueAmt
        {
            get
            {
                return sDueAmt;
            }
            set
            {
                if (value.IsNotNullOrEmpty())
                    sDueAmt = value;
            }
        }
        public string InstalAmt
        {
            get
            {
                return sInstalAmt;
            }
            set
            {
                if (value.IsNotNullOrEmpty())
                    sInstalAmt = value;
            }
        }
        public string Year
        {
            get
            {
                return sYear;
            }
            set
            {
                if (value.IsNotNullOrEmpty())
                    sYear = value;
            }
        }
    }
    private List<InstalData> lsBuildInstalDataArray(List<string> lsTaxDataLine)
    {
        // -- Create the struct and list to pick up all the install data so we can store it correctly later...
        InstalData sID                = new InstalData();
        List<InstalData> lsInstalData = new List<InstalData>();
        // -- Loop through the data, pull what we need, and build the array...
        foreach (string sLine in lsTaxDataLine)
        {
            sID.Year      = sGetParsedValue("Year", sLine);
            sID.InstalAmt = sGetParsedValue("Instal", sLine);
            sID.DueAmt    = sGetParsedValue("Due", sLine);
            // -- ONLY pick up data when there is 'Prinicipal' or 'Surchareg'...
            if ((FSH.Pos("Principal", sLine) > 0 || FSH.Pos("Surcharge", sLine) > 0) && sID.DueAmt != "0.00")
                lsInstalData.Add(sID);
        }   
        // -- Return the final array...
        return lsInstalData;
    }

这是一个单元测试,使用注释掉的代码将列表写入一个文件,用于我自己的调试目的,以显示数据是相同的。

[TestClass]
public class OR_CK_WTax_Tests
{
    // -- Create the instance of the Scrape for the public function(s)...
    OR_CK_WTax _Scrape;
    // -- Create the instance of the main class to create a link to the targeted private function(s)...
    PrivateObject poScrape = new PrivateObject(typeof(OR_CK_WTax));
    [TestInitialize]
    public void InitializeScript()
    {
        _Scrape = new OR_CK_WTax();
    }
    [TestMethod]
    public void OR_CK_WTax_lsBuildInstalDataArray_Success()
    {
        // -- Arrange...
        List<string> lsRecordList              = new List<string>();
        lsRecordList.Add("<TR><TD class=GridField>1993</TD><TD class=GridField>Property Tax Interest</TD><TD class=GridField>086-002</TD><TD class=GridFieldNumeric>2,502.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/1993</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>1994</TD><TD class=GridField>Property Tax Principal</TD><TD class=GridField>086-002</TD><TD class=GridFieldNumeric>3,148.75</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>200.00</TD><TD class=GridField>11/15/1994</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>1995</TD><TD class=GridField>Property Tax Principal</TD><TD class=GridField>086-002</TD><TD class=GridFieldNumeric>2,753.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>200.00</TD><TD class=GridField>11/15/1995</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>1996</TD><TD class=GridField>Property Tax Principal</TD><TD class=GridField>086-002</TD><TD class=GridFieldNumeric>2,925.70</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>200.00</TD><TD class=GridField>11/15/1996</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>1997</TD><TD class=GridField>Property Tax Principal</TD><TD class=GridField>086-002</TD><TD class=GridFieldNumeric>2,691.46</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>200.00</TD><TD class=GridField>11/15/1997</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>1998</TD><TD class=GridField>Property Tax Principal</TD><TD class=GridField>086-002</TD><TD class=GridFieldNumeric>2,745.44</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>200.00</TD><TD class=GridField>11/15/1998</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>1999</TD><TD class=GridField>Property Tax Principal</TD><TD class=GridField>086-002</TD><TD class=GridFieldNumeric>2,773.58</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>200.00</TD><TD class=GridField>11/15/1999</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2000</TD><TD class=GridField>Property Tax Interest</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>3,125.79</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2000</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2001</TD><TD class=GridField>Property Tax Interest</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>3,243.49</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2001</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2002</TD><TD class=GridField>Property Tax Interest</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>3,349.15</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2002</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2003</TD><TD class=GridField>Property Tax Interest</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>3,471.05</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2003</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2004</TD><TD class=GridField>Property Tax Interest</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>3,556.47</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2004</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2005</TD><TD class=GridField>Property Tax Interest</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>3,609.72</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2005</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2006</TD><TD class=GridField>Property Tax Interest</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>3,591.16</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2006</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2007</TD><TD class=GridField>Property Tax Interest</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>3,737.88</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2007</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2008</TD><TD class=GridField>Property Tax Interest</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>3,827.12</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2008</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2009</TD><TD class=GridField>Property Tax Interest</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>4,124.05</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2009</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2010</TD><TD class=GridField>Property Tax Principal</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>4,233.20</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2010</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2010</TD><TD class=GridField>Fire Patrol District Surcharge</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>4,333.20</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2010</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2011</TD><TD class=GridField>Fire Patrol District Surcharge</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>4,352.73</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2011</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2011</TD><TD class=GridField>Property Tax Interest</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>2,534.73</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2011</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2012</TD><TD class=GridField>Property Tax Principal</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>4,483.61</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2012</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2013</TD><TD class=GridField>Fire Patrol District Principal</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>4,630.42</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2013</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2013</TD><TD class=GridField>Fire Patrol District Surcharge</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>4,666.42</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>666.00</TD><TD class=GridField>11/15/2013</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2013</TD><TD class=GridField>Income Tax Principal</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>5,643.42</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2013</TD></TR><TR");
        lsRecordList.Add("R><TD class=GridField>2013</TD><TD class=GridField>Property Tax Principal</TD><TD class=GridField>086-042</TD><TD class=GridFieldNumeric>3,639.42</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridFieldNumeric>0.00</TD><TD class=GridField>11/15/2013</TD></TR><TR");
        List<OR_CK_WTax.InstalData> lsExpected = new List<OR_CK_WTax.InstalData>();
        OR_CK_WTax.InstalData sID              = new OR_CK_WTax.InstalData();
        sID.Year                               = "1994";
        sID.InstalAmt                          = "3,148.75";
        sID.DueAmt                             = "200.00";
        lsExpected.Add(sID);
        sID.Year                               = "1995";
        sID.InstalAmt                          = "2,753.00";
        sID.DueAmt                             = "200.00";
        lsExpected.Add(sID);
        sID.Year                               = "1996";
        sID.InstalAmt                          = "2,925.70";
        sID.DueAmt                             = "200.00";
        lsExpected.Add(sID);
        sID.Year                               = "1997";
        sID.InstalAmt                          = "2,691.46";
        sID.DueAmt                             = "200.00";
        lsExpected.Add(sID);
        sID.Year                               = "1998";
        sID.InstalAmt                          = "2,745.44";
        sID.DueAmt                             = "200.00";
        lsExpected.Add(sID);
        sID.Year                               = "1999";
        sID.InstalAmt                          = "2,773.58";
        sID.DueAmt                             = "200.00";
        lsExpected.Add(sID);
        sID.Year                               = "2013";
        sID.InstalAmt                          = "4,666.42";
        sID.DueAmt                             = "666.00";
        lsExpected.Add(sID);
        // -- Act...
        List<OR_CK_WTax.InstalData> lsActual = (List<OR_CK_WTax.InstalData>)poScrape.Invoke("lsBuildInstalDataArray", lsRecordList);   // -- Private
        // -- Assert...
        Assert.AreEqual(lsExpected, lsActual);
    }
}

感谢您提供的任何帮助!!

单元测试列表<;struct>;

您要求MSTest通过使用Assert.AreEqual(object, object)来断言两个列表是否引用相等。

考虑使用CollectionAssert类的方法,但您可能需要单独测试列表中的每个项目,并比较大小等。

没有神奇的"使用人类会使用的逻辑比较来测试这两个集合及其元素是否完全相同"功能。