测试比较数组方法失败

时间:2013-06-11 15:35:35

标签: c# unit-testing testing

我正在测试我的班级。我的方法返回给定策略ID的已支付保险百分比和财务限制的数组。 对于政策ID 43,其财务限额为空,其支付的保险百分比为95.75。我正在尝试测试这个,但我的测试仍然失败。谁能告诉我哪里做错了?

这是我的班级

public class PatientInsuranceLimits : System.Web.UI.Page
{

    String sqlConStr = ConfigurationManager.ConnectionStrings["connectionname"].ToString();


    public String[] generalLimits(String policyID)
    {
        String []resultset = new String[2];
        SqlConnection con = new SqlConnection(sqlConStr);
        String sqlQuery1 = "Select InsurancePaidPercentage as iPP, FinancialLimit as fLimit from Policy where ID=@poid";

        SqlCommand cmd1 = new SqlCommand(sqlQuery1, con);
       cmd1.Parameters.AddWithValue("@poid", policyID);

       try
       { 
        con.Open();
           SqlDataReader r = cmd1.ExecuteReader();
           while(r.Read()){
               resultset[0] = r[0].ToString();
               resultset[1] = r[1].ToString();

           }
           r.Close();
       }
       catch(Exception ex){

        }
        finally{
            con.Close();
       }

       return resultset;

    }

这是我的测试类

namespace _10_06_13_test

{
    [TestClass()]
    public class PatientInsuranceLimitsTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [DataSource("System.Data.SqlClient", "Data Source=servername_lab;Initial Catalog=IDKit;User ID=userid;Password=password", "mytable", DataAccessMethod.Sequential), TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("C:\\Users", "/")]
        [UrlToTest("http://localhost:51063/")]
        public void generalLimitsTest()
        {
            SurvivalHealth.PatientInsuranceLimits target = new SurvivalHealth.PatientInsuranceLimits(); 
            string policyID = "43";
            string[] expected = new string[] {"95.75"};
            string[] actual;
            actual = target.generalLimits(policyID);
            Assert.AreEqual(expected, actual);
        }
    }
}       

1 个答案:

答案 0 :(得分:1)

该函数返回一个包含null和“19.75”的数组。 您需要将分配给预期的值更改为{ null, "19.75"}

但是,如果您使用的是.net 2或更高版本,我建议您更改方法以返回元组以防止出现此类错误。将数字存储在十进制数字数据类型中也是一个好主意。