Exemplo n.º 1
0
        public async Task CreateAsync_WithValidModel_WorksCorrectly()
        {
            // Arrange
            var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>()
                                              .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                              .Options);

            var ride = new Ride
            {
                Title = "TestRide"
            };

            await context.Rides.AddAsync(ride);

            var user = new PoolItUser
            {
                UserName = "******"
            };

            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            var joinRequestsService = new JoinRequestsService(
                new EfRepository <JoinRequest>(context),
                new EfRepository <Ride>(context),
                new EfRepository <PoolItUser>(context),
                null);

            var serviceModel = new JoinRequestServiceModel
            {
                RideId = ride.Id,
                User   = new PoolItUserServiceModel
                {
                    UserName = user.UserName
                },
                Message = "Test Message"
            };

            // Act
            var result = await joinRequestsService.CreateAsync(serviceModel);

            // Assert
            Assert.True(result);

            var dbModel = await context.JoinRequests.SingleOrDefaultAsync();

            Assert.NotNull(dbModel);

            Assert.Equal(user.Id, dbModel.UserId);
        }
Exemplo n.º 2
0
        public async Task CreateAsync_WithInvalidModel_ReturnsFalse()
        {
            // Arrange
            var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>()
                                              .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                              .Options);

            var ride = new Ride
            {
                Title = "TestRide"
            };

            await context.Rides.AddAsync(ride);

            var user = new PoolItUser
            {
                UserName = "******"
            };

            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            var joinRequestsService = new JoinRequestsService(
                new EfRepository <JoinRequest>(context),
                new EfRepository <Ride>(context),
                new EfRepository <PoolItUser>(context),
                null);

            var serviceModel = new JoinRequestServiceModel
            {
                RideId = ride.Id,
                User   = new PoolItUserServiceModel
                {
                    UserName = user.UserName
                }
            };

            // Act
            var result = await joinRequestsService.CreateAsync(serviceModel);

            // Assert
            Assert.False(result);

            var existsInDb = await context.JoinRequests.AnyAsync();

            Assert.False(existsInDb);
        }