简体   繁体   English

Rhino Mocks期望未正确返回收藏

[英]Rhino Mocks expectation not returning collection correctly

I am new to Rhino Mocks, and using mock isolation frameworks in general for unit testing. 我是Rhino Mocks的新手,通常使用模拟隔离框架进行单元测试。 I have written the following test, where I have set up an expectation for a mock IDataProvider object to return a collection of objects. 我编写了以下测试,在该测试中,我为模拟IDataProvider对象设置了一个期望,以返回对象的集合。 The collection supplied has one object in it. 提供的集合中有一个对象。

When I run the test, the call to the IDataProvider returns a empty list when it should return the list with one object in it. 当我运行测试时,对IDataProvider的调用将返回一个空列表,该调用应返回其中包含一个对象的列表。

Any ideas whats going wrong? 任何想法出什么事了吗?

Here is my test: (Please excuse any bad practises here... feel free to mention any. Im trying to learn! Thanks) 这是我的测试:(请原谅这里的任何不良做法...请随时提及。我正在尝试学习!谢谢)

[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();
}

Relevant portion of method under test: (The DataProvider.GetDataSeries call returns empty list... this should return stubbed list.) 被测方法的相关部分:(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;
  }

I don't think we can tell the code provided, but are you sure your method under tests is calling GetDataSeries with the same params? 我认为我们无法说出所提供的代码,但是您确定测试中的方法正在调用具有相同参数的GetDataSeries吗? I'm especially curious about the first parameter, which in the mock is string.empty. 我对第一个参数特别好奇,该参数在模拟中为string.empty。 If you use IgnoreParameters() or one of the Is.Any() values you can narrow it down and see if this is the issue. 如果使用IgnoreParameters()或Is.Any()值之一,则可以缩小范围并查看是否是问题所在。

So maybe try this and see if it returns properly, then you can backtrack if this is the issue. 因此,也许可以尝试一下,看看它是否返回正确,然后可以回溯这是否是问题所在。

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

You're missing a call to ReplayAll: 您缺少对ReplayAll的通话:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rhino Mocks:如何在期望中匹配数组参数? - Rhino Mocks : How to match array arguments in an expectation? 犀牛嘲笑。 如何添加订阅事件处理程序的期望 - Rhino Mocks. How to add expectation that event handler was subscribed Rhino Mocks:实例化 Mock 属性,以便 Expectation 可以引用它 - Rhino Mocks: Instantiating Mock property so Expectation can reference it 如何将期望中的参数传递给Rhino Mocks中的返回值? - How to pass a parameter in an expectation to the return value in Rhino Mocks? Rhino Mocks IgnoreArguments()并测试是否正确调用了lambda - Rhino Mocks IgnoreArguments() and testing if a lambda was called correctly Rhino Mocks - 使用ref / out参数模拟集合 - Rhino Mocks - Mocking collection with ref/out arguments Rhino Mocks:如何在不明确所有期望的情况下更改方法,属性或字段的期望? - Rhino Mocks : How to change expectation on method, property or field without clear all expectation? 如何正确使用Rhino.Mocks AssertWasCalled()? - How to use Rhino.Mocks AssertWasCalled() correctly? 使用Rhino Mocks返回空列表作为默认值 - Returning empty lists as default with Rhino Mocks Rhino模拟无法对模拟对象的第二次调用设定期望,该对象的接口返回一个Func <T> - Rhino mocks is unable to set an expectation on the 2nd call of mocked object whose interface returns a Func<T>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM