我正在尝试使用AutoFixture
模拟特定方法的数据。
_dataProvider = Substitute.For<IEstimationDataProvider>();
var rateTypes = _fixture.Build<RateType>().CreateMany(12).ToList(); ***** ERROR LINE.
_dataProvider.GetSeasonalPrices(rfg).Returns( rateTypes );
方法:
public async Task<List<RateType>> GetSeasonalPrices(string rfg)
{
var results = await _seasonalRateTypeRepository.GetByPartitionAsync(rateFactGroup);
var seasonalRate = results.First();
return new List<RateType>
{
seasonalRate.Jan,
seasonalRate.Feb,
seasonalRate.Mar,
seasonalRate.Apr,
seasonalRate.May,
seasonalRate.Jun,
seasonalRate.Jul,
seasonalRate.Aug,
seasonalRate.Sep,
seasonalRate.Oct,
seasonalRate.Nov,
seasonalRate.Dec
};
}
public enum RateType
{
OffPeakRate,
PeakRate
}
以下是实际错误:
Inner exception messages:
AutoFixture.ObjectCreationException: The decorated ISpecimenBuilder could not create a specimen based on the request: ABC.Estimation.ABC.Models.Repository.RateType. This can happen if the request represents an interface or abstract class; if this is the case, register an ISpecimenBuilder that can create specimens based on the request. If this happens in a strongly typed Build<T> expression, try supplying a factory using one of the IFactoryComposer<T> methods.
答案 0 :(得分:1)
几次试用后,我发现以下解决方案。
var rateTypes = _fixture.CreateMany<RateType>(12).ToList();
虽然不确定到底是什么引起了问题。