【问题标题】:Rhino Mocks expectation not returning collection correctlyRhino Mocks 期望没有正确返回集合
【发布时间】:2009-12-04 23:46:08
【问题描述】:

我是 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;
  }

【问题讨论】:

  • 也许它在您正在测试的代码中发现了一个错误?您也可以发布 FetchDataSeries 的代码吗?
  • IDataProvider.GetDataSeries 是重载的方法,还是只有一个具有该名称的方法?在实现中传递给方法的 DataQuery 参数是什么?

标签: c# unit-testing rhino-mocks


【解决方案1】:

我认为我们无法分辨所提供的代码,但您确定您的测试方法是使用相同的参数调用 GetDataSeries 吗?我对第一个参数特别好奇,在mock中是string.empty。如果您使用 IgnoreParameters() 或 Is.Any() 值之一,您可以缩小范围,看看这是否是问题所在。

所以也许试试这个,看看它是否正确返回,如果是这个问题,你可以回溯。

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

【讨论】:

  • 是的,我认为参数是不同的,但经过检查,它们与预期的相同,但无论如何 IgnoreParameters 会更好地排除这种情况......谢谢!跨度>
【解决方案2】:

您错过了对 ReplayAll 的调用:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多