[英]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.