public async Task GenerateAsync_WithCorrectRideId_WorksCorrectly() { // Arrange const string expectedKey = "expkey"; var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options); var ride = new Ride { Title = "TestRide" }; await context.Rides.AddAsync(ride); await context.SaveChangesAsync(); var mockRandomStringGeneratorHelper = new Mock <IRandomStringGeneratorHelper>(); mockRandomStringGeneratorHelper.Setup(r => r.GenerateRandomString(It.IsAny <int>())).Returns(expectedKey); var invitationsService = new InvitationsService(new EfRepository <Invitation>(context), new EfRepository <Ride>(context), null, null, null, mockRandomStringGeneratorHelper.Object); // Act var resultKey = await invitationsService.GenerateAsync(ride.Id); // Assert Assert.Equal(expectedKey, resultKey); var invitationExists = await context.Invitations.AnyAsync(i => i.Key == resultKey); Assert.True(invitationExists); }
public async Task GenerateAsync_WithNonExistentRide_ReturnsNull() { // Arrange var testRideId = Guid.NewGuid().ToString(); var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options); var mockRandomStringGeneratorHelper = new Mock <IRandomStringGeneratorHelper>(); mockRandomStringGeneratorHelper.Setup(r => r.GenerateRandomString(It.IsAny <int>())).Returns("abcdef"); var invitationsService = new InvitationsService(new EfRepository <Invitation>(context), new EfRepository <Ride>(context), null, null, null, mockRandomStringGeneratorHelper.Object); // Act var resultKey = await invitationsService.GenerateAsync(testRideId); // Assert Assert.Null(resultKey); var invitationCount = await context.Invitations.CountAsync(); Assert.Equal(0, invitationCount); }