按上下文类型划分的模拟方法依赖项.单元测试中的 Database.ExecuteSql命令

本文关键字:单元测试 Database 命令 ExecuteSql 方法 类型 上下文 划分 模拟 依赖 | 更新日期: 2023-09-27 17:56:16

我正在编写一个调用 FillInventory 方法的单元测试,在此方法中,某些内容将在数据库中更新,在单元测试中,我想通过调用 FillInventory 方法检查数据库中更新前后的数据中的某些内容并检查其输入。

在我的服务中:

public void FillInventory(InventoryViewModel invViewModel)
{
    long roomServiceId = invViewModel.RoomServiceId;
    var roomService = _unitOfWork.RoomServiceRepository.GetByID(roomServiceId);
    var placeId = roomService.Room.PlaceId;
    var capacity = roomService.Room.Capacity + roomService.ExtraCapacity;
    for (DateTime date = invViewModel.StartDate; date.Date < invViewModel.EndDate; date = date.AddDays(1))
    {
        var inv = _unitOfWork.InventoryRepository.GetByKey(invViewModel.RoomServiceId, date);
        if (inv != null)
        {
            inv.Price =(invViewModel.isUpdatingPrice)? invViewModel.Price:inv.Price;
            inv.BoardPrice = (invViewModel.isUpdatingBoardPrice) ? invViewModel.BoardPrice : inv.BoardPrice;
            inv.CertainAvailability = (invViewModel.isUpdatingCertainAvailability) ? invViewModel.CertainAvailability : inv.CertainAvailability;
            inv.FloatAvailability = (invViewModel.isUpdatingFloatAvailability) ? invViewModel.FloatAvailability : inv.FloatAvailability;
            inv.setWebServices(invViewModel.SellableWebServices.Select(sw => sw.Key).ToList());
        }
        else
        {
            var price = (invViewModel.isUpdatingPrice) ? invViewModel.Price : 0;
            var boardPrice = (invViewModel.isUpdatingBoardPrice) ? invViewModel.BoardPrice : 0;
            var certainAvailability = (invViewModel.isUpdatingCertainAvailability) ? invViewModel.CertainAvailability : 0;
            var floatAvailability = (invViewModel.isUpdatingFloatAvailability) ? invViewModel.FloatAvailability : 0;

            inv = new Jabama.Core.DataLayer.Models.Inventory(roomService, date, floatAvailability, certainAvailability,
                price, boardPrice);
            _unitOfWork.InventoryRepository.Insert(inv);
        }

    }
    _unitOfWork.Save();
    _unitOfWork.PlaceInfoRepository.UpdatePlaceInfos(placeId, capacity, invViewModel.StartDate, invViewModel.EndDate);
}

在我的存储库中

public class PlaceInfoRepository : GenericRepository<PlaceInfo>
{
    public PlaceInfoRepository(JabamaContext context):base(context)
    { }
    public void UpdatePlaceInfos(long placeId, int capacity, DateTime startDate, DateTime endDate)
    {
        var cmd = context.Database.Connection.CreateCommand();
        List<SqlParameter> p = new List<SqlParameter>();
        p.Add(new SqlParameter("@placeId", placeId));
        p.Add(new SqlParameter("@capacity",capacity));
        p.Add(new SqlParameter("@startDate", startDate.Date.ToString("yyyy -MM-dd")));
        p.Add(new SqlParameter("@endDate", endDate.Date.ToString("yyyy-MM-dd")));
        context.Database.ExecuteSqlCommand("exec UpdatePlaceInfo @placeId, @capacity, @startDate, @endDate", p.ToArray());
    }
}

我已经模拟了单元测试需要模拟的所有内容,但是现在当存储库想要使用context.Database.ExecuteSqlCommand更新数据时出现问题。

我怎么能模拟它?

按上下文类型划分的模拟方法依赖项.单元测试中的 Database.ExecuteSql命令

无需模拟/伪造DbContext及其成员 - 您只需模拟PlaceInfoRepository.

您的目标是检查FillInventory是否使用适当的参数调用UpdatePlaceInfos - 仅此而已

// Arrange
var repositoryMock = new SomeMock<IPlaceInfoRepository>();
... // Setup mock for verification.
var service = new Service(repositoryMock.Object);
// Act
service.FillInventory(vm);
// Assert
repositoryMock.Verify();

其他任何事情,它都更接近模块集成测试,虽然在许多情况下有用,但有时更难做到,并且通常比单个代码单元具有更大的范围。

相关阅读 - http://martinfowler.com/articles/mocksArentStubs.html