簡體   English   中英

Rhino Mocks期望未正確返回收藏

[英]Rhino Mocks expectation not returning collection correctly

我是Rhino Mocks的新手,通常使用模擬隔離框架進行單元測試。 我編寫了以下測試,在該測試中,我為模擬IDataProvider對象設置了一個期望,以返回對象的集合。 提供的集合中有一個對象。

當我運行測試時,對IDataProvider的調用將返回一個空列表,該調用應返回其中包含一個對象的列表。

任何想法出什么事了嗎?

這是我的測試:(請原諒這里的任何不良做法...請隨時提及。我正在嘗試學習!謝謝)

[TestMethod()]
public void FetchDataSeries_NeedsUpdate_SuccessfulDataSeriesRetrievedFromDataProvider() {
  List<IDataSeries> dataSeries = new List<IDataSeries>();
  dataSeries.Add(new DataSeries("test"));
  DrillDownLevel level = DrillDownLevel.YEAR;
  int? year = 2008;

  var dataProvider = _MockRepository.CreateMock<IDataProvider>();
  dataProvider.Expect(dp => dp.GetDataSeries(String.Empty, level, year, null ,null, null)).Return(dataSeries);
  _DataSourceContext.DataProvider = dataProvider;

  CollectionAssert.AreEqual(dataSeries, _DataSourceContext.FetchDataSeries(level, year, null, null, null));
  dataProvider.VerifyAllExpectations();
}

被測方法的相關部分:(DataProvider.GetDataSeries調用返回空列表...這應該返回存根列表。)

      public override List<IDataSeries> FetchDataSeries(DrillDownLevel? drillDownLevel, int? year, int? month, DateTime? week, int? day) {

    List<IDataSeries> dataSeries = new List<IDataSeries>();

    // Cache data for maximum cache period
    // if data has been cached for longer than the maxium cache period OR the updateInterval has elapsed UNLESS LastUpdateAttempt was less than minimum update interval
    if (NeedsUpdate(LastUpdate, LastUpdateAttempt)) {

      // Attempt to get new data
      LoggingService.InfoFormat("DataSourceContext: {0}: Attempting to get new data:", Name);
      dataSeries = DataProvider.GetDataSeries(DataQuery, drillDownLevel, year, month, week, day);
    }

    return dataSeries;
  }

我認為我們無法說出所提供的代碼,但是您確定測試中的方法正在調用具有相同參數的GetDataSeries嗎? 我對第一個參數特別好奇,該參數在模擬中為string.empty。 如果使用IgnoreParameters()或Is.Any()值之一,則可以縮小范圍並查看是否是問題所在。

因此,也許可以嘗試一下,看看它是否返回正確,然后可以回溯這是否是問題所在。

dataProvider.Expect(dp => dp.GetDataSeries(String.Empty, level, year, null ,null, null)).IgnoreParameters().Return(dataSeries);

您缺少對ReplayAll的通話:

    _MockRepository.ReplayAll();
    CollectionAssert.AreEqual(dataSeries, _DataSourceContext.FetchDataSeries(level, year, null, null, null));

參見: http : //ayende.com/Wiki/Comparison+of+different+Rhino+Mocks+syntaxes.ashx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM