public async Task CreateGuestAsync_ReturnErrorResponse_IfExceptionThrown() { // Arrange var mockGuestDto = new GuestDto { FirstName = "John", LastName = "Doe", Age = 30 }; var mockGuest = new Guest { FirstName = "John", LastName = "Doe", Age = 30 }; var mockMapper = new Mock <IMapper>(); mockMapper.Setup(x => x.Map <Guest>(mockGuestDto)).Returns(mockGuest); var mockRepo = new Mock <IGuestRepository>(); mockRepo.Setup(x => x.CreateAsync(mockGuest)).Throws(new Exception("test message")); // Act var service = new GuestService(mockMapper.Object, mockRepo.Object); var result = await service.CreateAsync(mockGuestDto); // Assert result.Should().BeOfType <GuestResponse>(); result.Result.Should().BeFalse(); result.Status.Should().Be(ResponseStatus.Error); result.Item.Should().BeNull(); result.Message.Should().Contain("test message"); }
public async Task CreateGuestAsync_ReturnSuccessResponse_ForValidGuestDto() { // Arrange var mockGuestDto = new GuestDto { FirstName = "John", LastName = "Doe", Age = 30 }; var mockGuest = new Guest { FirstName = "John", LastName = "Doe", Age = 30 }; var mockMapper = new Mock <IMapper>(); mockMapper.Setup(x => x.Map <Guest>(mockGuestDto)).Returns(mockGuest); var mockRepo = new Mock <IGuestRepository>(); // Act var service = new GuestService(mockMapper.Object, mockRepo.Object); var result = await service.CreateAsync(mockGuestDto); // Assert result.Should().BeOfType <GuestResponse>(); result.Result.Should().BeTrue(); result.Status.Should().Be(ResponseStatus.Success); result.Item.Id.Should().Be(mockGuest.Id); result.Item.FirstName.Should().Be(mockGuest.FirstName); result.Item.LastName.Should().Be(mockGuest.LastName); }
public async Task CreateGuestAsync_CreatesGuest_IfGuestDtoCorrect() { // Arrange var mapperConfig = new MapperConfiguration(cfg => cfg.AddProfile <MappingProfile>()); var mapper = mapperConfig.CreateMapper(); var options = await SetupTestDatabaseAsync(_guests); var httpContextAccessor = new Mock <IHttpContextAccessor>(); var guestRepository = new GuestRepository(new WeddingPlannerDbContext(options, httpContextAccessor.Object)); var guestService = new GuestService(mapper, guestRepository); // Act var guestDto = new GuestDto { FirstName = "John", LastName = "Doe" }; await guestService.CreateAsync(guestDto); // Assert using var context = new WeddingPlannerDbContext(options, httpContextAccessor.Object); var guests = await context.Guests.ToListAsync(); var guest = await context.Guests.SingleAsync(x => x.Id == 4); guest.Should().NotBeNull(); guest.Id.Should().Be(4); guest.FirstName.Should().Be("John"); guest.LastName.Should().Be("Doe"); }
public async Task CreateGuestAsync_ReturnErrorResponse_ForNullParam() { // Arrange GuestDto mockGuestDto = null; Guest mockGuest = null; var mockMapper = new Mock <IMapper>(); mockMapper.Setup(x => x.Map <Guest>(mockGuestDto)).Returns(mockGuest); var mockRepo = new Mock <IGuestRepository>(); // Act var service = new GuestService(mockMapper.Object, mockRepo.Object); var result = await service.CreateAsync(mockGuestDto); // Assert result.Should().BeOfType <GuestResponse>(); result.Result.Should().BeFalse(); result.Status.Should().Be(ResponseStatus.Error); result.Item.Should().BeNull(); }