From 8215dbf3c6ffef811ddbdd40d81ba51ebc6d1560 Mon Sep 17 00:00:00 2001 From: Matthias Beerens <3512339+Matthiee@users.noreply.github.com> Date: Sun, 24 Jul 2022 14:11:30 +0200 Subject: [PATCH 1/2] End of tutorial --- Tests/ReservationSystemTests.cs | 39 +++++++++++++++++++++++++++++++-- Tests/Tests.csproj | 1 + 2 files changed, 38 insertions(+), 2 deletions(-) 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 From c36b277d5f2d23d9de62adc8cbd4a830201805ae Mon Sep 17 00:00:00 2001 From: Matthias Beerens <3512339+Matthiee@users.noreply.github.com> Date: Mon, 25 Jul 2022 16:56:37 +0200 Subject: [PATCH 2/2] Add link --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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