views:

65

answers:

2

I am new to Rhino Mocks, and using mock isolation frameworks in general for unit testing. I have written the following test, where I have set up an expectation for a mock IDataProvider object to return a collection of objects. 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.

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.)

      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;
  }
+2  A: 

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? I'm especially curious about the first parameter, which in the mock is 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.

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);
Scott Kirkland
yeah I considered the params to be diff, but on inspection they are the same as what the expectation is looking for, but IgnoreParameters would be better anyway just to rule that out... thanks!
theringostarrs
+1  A: 

You're missing a call to 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

Mark Byers
BINGO! you got it!
theringostarrs