0

我的函数中有以下代码来从数据库中检索数据:

var oldFareSpecification = new ObservationOnOrBeforeDateSpecification(previousOutboundDate));

var oldFare = _fareRepository.Find(oldFareSpecification).SingleOrDefault();

规范是:

public class ObservationOnOrBeforeDateSpecification : Specification<Fare>
    {
        public ObservationOnOrBeforeDateSpecification(DateTime date)
        : base(fh => EntityFunctions.DiffDays(fh.ObservationTS, date) >= 0)
    {
    }
    }

它使用 System.Data.Entity。
存储库是

Repository<TEntity> : IRepository<TEntity>
private readonly DbContext _context;
        private readonly DbSet<TEntity> _entitySet;
public IEnumerable<TEntity> Find(ISpecification<TEntity> criteria)
        {
            return criteria.SatisfyingEntitiesFrom(_entitySet).AsEnumerable();
        }

它工作正常,但是当我尝试在测试类中使用 Moq 存储库并调用存储库返回对我的假对象的查询结果时,我得到一个异常:函数只能从 LINQ 调用到实体(或非常相似 - 我明白了用其他语言:p)。

我的测试类使数据库以这种方式返回假结果:

var faresRepository = Fixture.Freeze<Mock<IRepository<DAL.Fare>>>();
faresRepository.Setup(
                fhr => fhr.Find(It.IsAny<ISpecification<Fare>>())).Returns(
                (ISpecification<Fare> spec) => spec.SatisfyingEntitiesFrom(fares.AsQueryable()));

现在我的问题是如何编写代码规范来比较天数的差异,并且可以正常从代码和 Mocked 存储库中调用。通过正确比较,我的意思是,2013.01.16 23:55:34 和 2013.01.17 01:55:34 天数之间的差异返回 1。

4

1 回答 1

0

解决方案:

** 唯一的方法是创建一个队列,该队列在每次调用特定方法(此处为 .Find)时返回假结果。不完全是我想要实现的目标,但至少有效。所以这里是:

    var faresHistoryRepository = Fixture.Freeze<Mock<IRepository<DAL.FareHistory>>>();

    var fareHistoryResults = new Queue<List<FareHistory>>();

    var fareHistories = new List<DAL.FareHistory>();
    FareHistory fh1 = new FareHistory { FareHistoryId = fareHistoryId + 1, FareId = fareId, ObservationTS = date.AddDays(-1), StatusId = (int)DAL.Enum.Status.Active };
    fareHistories.Add(fh1);
    fh1 = new FareHistory { FareHistoryId = fareHistoryId + 4, FareId = fareId, ObservationTS = date.AddDays(-4), StatusId = (int)DAL.Enum.Status.Active };
    fareHistories.Add(fh1);
    fh1 = new FareHistory { FareHistoryId = fareHistoryId + 5, FareId = fareId, ObservationTS = date.AddDays(-5), StatusId = (int)DAL.Enum.Status.Active };
    fareHistories.Add(fh1);
    fareHistoryResults.Enqueue(fareHistories); // fare histories

    var fareHistoriesSample = new List<DAL.FareHistory>();
    fh1 = new FareHistory { FareHistoryId = fareHistoryId + 6, FareId = oldFareId,
 ObservationTS = depDate.AddDays(-fullDaysDifference), StatusId = (int)DAL.Enum.Status.Active };
    fareHistoriesSample.Add(fh1);
        fh1 = new FareHistory { FareHistoryId = fareHistoryId + 7, FareId = oldFareId,
     ObservationTS = depDate.AddDays(-15), StatusId = (int)DAL.Enum.Status.Active };
        fareHistoriesSample.Add(fh1);
        fh1 = new FareHistory { FareHistoryId = fareHistoryId + 8, FareId = oldFareId,
     ObservationTS = depDate.AddDays(-16), StatusId = (int)DAL.Enum.Status.Active };
                    fareHistoriesSample.Add(fh1);

        fareHistoryResults.Enqueue(fareHistoriesSample); // fareHistoriesSample

        faresHistoryRepository.Setup(
                            fhr => fhr.Find(It.IsAny<ISpecification<FareHistory>>
    ())).Returns(()=>fareHistoryResults.Dequeue());
于 2013-01-24T10:44:19.483 回答