模拟存储库,linq到实体函数,sqlfunctions

时间:2013-01-17 10:01:25

标签: c# linq-to-entities mocking

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

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.

1 个答案:

答案 0 :(得分: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());