创建列表时AutoFixture.ObjectCreationException

问题描述 投票:0回答:1

我试图使用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.
autofixture
1个回答
1
投票

几次尝试后我找到了以下解决方案。

var rateTypes = _fixture.CreateMany<RateType>(12).ToList();

不知道究竟是什么导致了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.