diff --git a/README.md b/README.md index 5f01d03..35963c3 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ -MockingTutorial \ No newline at end of file +# C# Mocking in Unit Testing using Moq +This repository contains the source code for unit testing in C# Tutorial Series on YouTube. + +https://youtu.be/jkzJwTbcSRg diff --git a/Tests/ReservationSystemTests.cs b/Tests/ReservationSystemTests.cs index 6e8fbc4..c5e7396 100644 --- a/Tests/ReservationSystemTests.cs +++ b/Tests/ReservationSystemTests.cs @@ -14,22 +14,57 @@ namespace Tests public class ReservationSystemTests { private readonly ReservationSystem reservationSystem; + private readonly Mock bookingServiceMock = new(); public ReservationSystemTests() { + //reservationSystem = new ReservationSystem( + // new BookingService(new BookingDbContext())); + reservationSystem = new ReservationSystem( - new BookingService(new BookingDbContext())); + bookingServiceMock.Object); } [Fact] public async Task ReserveAsync_Throws_When_No_Rooms_Available() { - + bookingServiceMock + .Setup(mock => mock.GetRoomsAsync(10)) + .ReturnsAsync(Enumerable.Empty()); + + await Assert.ThrowsAsync( + () => reservationSystem.ReserveAsync(10)); } [Fact] public async Task ReserveAsync_Reserves_The_Available_Room() { + var reservedRoom = new Room + { + Beds = 10, + IsReserved = true, + RoomName = "Room 1" + }; + + var emptyRoom = new Room + { + Beds = 10, + IsReserved = false, + RoomName = "Room 2" + }; + + bookingServiceMock + .Setup(mock => mock.GetRoomsAsync(10)) + .ReturnsAsync(new[] + { + reservedRoom, + emptyRoom, + }); + + await reservationSystem.ReserveAsync(10); + + bookingServiceMock + .Verify(mock => mock.ReserveRoomAsync(emptyRoom.RoomId), Times.Once()); } } } \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index fbc6f84..7aed341 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -9,6 +9,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive