无法在单元测试中检索数据

本文关键字:检索 数据 单元测试 | 更新日期: 2023-09-27 18:35:27

我有一个关于检索数据的问题,这是我正在尝试学习的东西,也是第一次使用它。

测试

失败错误:"测试方法 TestBusinessLogic.MediaDurationBLTest.OpenModelTest抛出了异常: 系统异常: 无法检索媒体持续时间模型 另一个用户 已经更新了模型。请刷新并重试。

MediaDurationBLTest.cs

这是主要方法:

  [TestMethod()]
        public void OpenModelTest()
        {
            MediaDurationDS mds = new MediaDurationDS();
            PopulateTestDataSet(mds);
            MediaDurationBL target = new MediaDurationBL();
            TestBusinessLogic.BusinessLogic_MediaDurationBLAccessor accessor = new TestBusinessLogic.BusinessLogic_MediaDurationBLAccessor(target);
            //assign accessor to mds
            accessor.mMediaDurationDataSet = mds;
            int modelID = 5514;
            target.OpenModel(modelID);
            Assert.AreEqual(20, mds.Tables.Count, "# of tables retrieved are different");
            //We are creating copy of ProjectMetricData, check if copyTable and original table are same
            //except projectmetrictdata has pf&d and client does not so subtract that.
            int pfanddRows = 2;
            int projectMetricDataRows = accessor.mMediaDurationDataSet.ProjectMetricData.Rows.Count;
            int copiedRows = projectMetricDataRows - pfanddRows;
            if (copiedRows < 0)
                copiedRows = 0;
            Assert.AreEqual(accessor.mMediaDurationDataSet.ClientProjectMetricData.Rows.Count, copiedRows, "project metric data copy not created");
        }

这是"目标"的内部代码。OpenModel(modelID);",我得到错误并立即跳转到"catch (Exception e)",我的数据在这里为空"mMediaDurationDataLayer.GetModelDetails(mMediaDurationDataSet,modelID);",我该如何解决错误?

public DataSet OpenModel(int modelID)
        {
            try
            {
                mMediaDurationDataSet = new MediaDurationDS();
                mMediaDurationDataLayer.GetModelDetails(mMediaDurationDataSet, modelID);
                //ConvertToLocalTime(mMediaDurationDataSet.Model, "ClientLastUpdateDate");
                ConvertToLocalTime(mMediaDurationDataSet.ModelActivity, "ClientLastUpdateDate");
                //IF MODEL IS MOR, ACT
                CreateProjectForMORModel(modelID);
                //COPY PROJECT METRIC DATA TABLE INTO CLIENTPROJECTMETRICDATA   
                foreach (MediaDurationDS.ProjectMetricDataRow pmdr in mMediaDurationDataSet.ProjectMetricData.Rows)
                {
                    //WE DONT WANT PF&D IN CLIENT TABLE
                    if (!pmdr.MetricTypeName.Equals(PFANDDPARAMETER))
                    {
                        CreateClientProjectMetricDataRow(pmdr, pmdr.ProjectMetricID);
                    }
                }           
                mMediaDurationDataSet.AcceptChanges();
                mMediaDurationDataSet.WriteXml("C:''MediaDurationTestDataSet.xml");
                return mMediaDurationDataSet;
            }
            catch (Exception e)
            {
                string errorMessage = "Unable to retrieve Media Duration Model " +Environment.NewLine + e.Message;
                throw new Exception(errorMessage);
            }
        }

public class MediaDurationDL
{
    ProjectManagerDL mProjectManagerDL;
    public void GetModelDetails(DataSet mediaDurationDataSet, int modelID)
    {
        Database db = X.XXX.WindowsApplicationTemplate.ApplicationDatabase.DatabaseFactory.CreateDatabase();
        string sqlProcedure = "uspMediaDurationGetModel";
        DbCommand dbCommand = db.GetStoredProcCommand(sqlProcedure);
        UtilityDL.SetCommandTimeout(dbCommand);
        db.AddInParameter(dbCommand, "ModelID", DbType.Int32, modelID);
        string[] tables = new string[] { "LaborCategory", "ProcessCategory", "Media", "Activity",                   "Time",   "Model", "ModelTime", "ModelActivity", 
         "Project", "ProjectAccess", "MetricType", "ProjectMetric", "ProjectMetricData" };//, "Metric", "MetricData"};
        // RETRIEVE DATA FROM DB AND LOAD INTO DATASET
        mediaDurationDataSet.Clear();
        //PrintAllErrs(mediaDurationDataSet);
        db.LoadDataSet(dbCommand, mediaDurationDataSet, tables);
        //PrintAllErrs(mediaDurationDataSet);
    }

我猜"MediaDurationTestDataSet.xml"文件中存在不正确的数据信息,如何检索正确的数据信息?

无法在单元测试中检索数据

以您的异常为例,您在测试中执行此操作一次,在 OpenModel 中执行此操作一次,我认为您想在此处访问两次内容:

在测试设置中:

    MediaDurationDS mds = new MediaDurationDS();
    PopulateTestDataSet(mds);

在开放模型中:

    mMediaDurationDataSet = new MediaDurationDS();
    mMediaDurationDataLayer.GetModelDetails(mMediaDurationDataSet, modelID);

作为例外说:

无法检索媒体持续时间模型 其他用户已经 更新了模型。

因此,您的 PopulateTestDataSet 正在访问底层系统,并且您会收到异常,因为它正在使用中。