如何从单元测试中消除对数据库的依赖性

时间:2016-02-02 14:57:35

标签: c# unit-testing tdd

是否可以为此方法创建单元测试?

public void CheckIndicatorMeasurementRecordingValidity(IndicatorMeasurementRecording indicatorMeasurementRecording)
{
   var isDateRangeOverLapping = this.db.IndicatorMeasurementRecordingRepository.Any(r => r.IndicatorId == indicatorMeasurementRecording.Indicator.Id && r.Id != indicatorMeasurementRecording.Id
                && r.RecordingDateRange.ToDateTime >= indicatorMeasurementRecording.RecordingDateRange.FromDateTime
                && r.RecordingDateRange.FromDateTime <= indicatorMeasurementRecording.RecordingDateRange.ToDateTime);

   if (isDateRangeOverLapping)
   {
       throw new GrcUserException(IndicatorThresholdValidityManagementLocal.DateRangeOverlapping);
   }
}

我试图像这样创建测试

[TestMethod]
public void Indicator_measurement_date_range_overlapping_shall_throw_exception()
{
    var indicator = new Indicator();
    var oldIndicatorMeasurementRecording = new IndicatorMeasurementRecording();
    var newIndicatorMeasurementRecording = new IndicatorMeasurementRecording();
    var errorMessage = string.Empty;

    oldIndicatorMeasurementRecording.Id = 1;
    oldIndicatorMeasurementRecording.RecordingDateRange.FromDateTime = DateTime.Today.AddDays(-5);
    oldIndicatorMeasurementRecording.RecordingDateRange.ToDateTime = DateTime.Today;
    indicator.IndicatorMeasurementRecordings.Add(oldIndicatorMeasurementRecording);

    oldIndicatorMeasurementRecording.Id = 2;
    newIndicatorMeasurementRecording.RecordingDateRange.FromDateTime = DateTime.Today.AddDays(-2);
    newIndicatorMeasurementRecording.RecordingDateRange.ToDateTime = DateTime.Today;
    newIndicatorMeasurementRecording.Indicator = indicator;

    try
    {
        this.indicatorValidityProxy.CheckIndicatorMeasurementRecordingValidity(newIndicatorMeasurementRecording);
    }
    catch (Exception ex)
    {
        errorMessage = ex.Message;
    }

    Assert.AreEqual(IndicatorThresholdValidityManagementLocal.DateRangeOverlapping, errorMessage);
}

问题是,测试总是失败,因为 用于测试的this.db.IndicatorMeasurementRecordingRepository.Count始终为0。 出于开发目的,它始终返回正确的数字。只有在测试目的是0。

有谁能告诉我为什么this.db.IndicatorMeasurementRecordingRepository.Count的测试目的总是0?为我的案例编写测试的最佳做法是什么?

1 个答案:

答案 0 :(得分:1)

我不确定.db是什么,但假设public class MyContext : DbContextthis.db的类型为MyContext,那么您可以使用Mock:

var contextMock = new Mock<MyContext>();
// arrange more

基本上,当查询IndicatorMeasurementRecording时,您会将contextMock配置为返回一组已知对象(IndicatorMeasurementRecordingRepository?)。类似的东西:

var dbSetMock = new Mock<IDbSet<DbEntity>>();
dbSetMock.Setup(m => m.Provider).Returns(data.Provider);
dbSetMock.Setup(m => m.Expression).Returns(data.Expression);
dbSetMock.Setup(m => m.ElementType).Returns(data.ElementType);
dbSetMock.Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

var contextMock = new Mock<MyContext>();
contextMock 
    .Setup(x => x.DbEntities)
    .Returns(dbSetMock.Object);

数据将是从模拟中返回的对象列表。

一些链接/示例可以帮助您:

我无法重新创建您的测试用例,因为没有太多代码,但如果您提供mcve我非常乐意更详细地查看测试。