0

I'm writing a unitTest that call the FillInventory Method which in this method something will be update in database and in the Unit Test I want to check something in data before and after updating in DB by calling FillInventory Method and check its inputs.

In my service :

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);
}

In my repository

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());
    }
}

I have mocked everything that unit test needed to mock but now there is a problem when repository wants to update data with context.Database.ExecuteSqlCommand.

How can I mock it?

Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
samira riazati
  • 515
  • 7
  • 21

1 Answers1

1

No need to mock/fake the DbContext and its members - you just mock the PlaceInfoRepository.

You goal is to check that FillInventory calls the UpdatePlaceInfos with proper arguments - nothing more, nothing less:

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

// Act
service.FillInventory(vm);

// Assert
repositoryMock.Verify();

Anything else and it would be closer to a module integration test, that while useful in many cases is sometimes more difficult to do and usually has a bigger scope than a single unit of code.

Related reading - http://martinfowler.com/articles/mocksArentStubs.html

Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53