public async Task <User> CreateAsync(User user)
        {
            user.Id        = Guid.NewGuid();
            user.CreatedOn = DateTime.Now;

            await _dbContext.Users.AddAsync(user);

            await _dbContext.SaveChangesAsync();

            return(user);
        }
        public async Task GetAsync_ReturnsUsers()
        {
            // Arrange
            var dbContextOptions = DbContextHelper.GetOptions <WebApiCoreSeedContext>();
            var users            = new List <User>
            {
                GetSampleUser(),
                GetSampleUser(),
                GetSampleUser()
            };

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                await dbContext.Users.AddRangeAsync(users);

                await dbContext.SaveChangesAsync();
            }

            // Act
            List <User> result;

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                var userService = new UserService(dbContext);
                result = await userService.GetAsync();
            }

            // Assert
            Assert.Equal(users.Count, result.Count);
            foreach (var user in result)
            {
                var expected = users.SingleOrDefault(u => u.Id == user.Id);
                AssertUser(expected, user);
            }
        }
        public async Task DeleteAsync_WhenUserExists_ReturnsTrue()
        {
            // Arrange
            var dbContextOptions = DbContextHelper.GetOptions <WebApiCoreSeedContext>();
            var user             = GetSampleUser();

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                await dbContext.Users.AddAsync(user);

                await dbContext.SaveChangesAsync();
            }

            // Act
            bool result;
            User expectedUser;

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                var userService = new UserService(dbContext);
                result = await userService.DeleteAsync(user.Id);

                expectedUser = dbContext.Users.SingleOrDefault(u => u.Id == user.Id);
            }

            // Assert
            Assert.True(result);
            Assert.Null(expectedUser);
        }
        public async Task GetByIdAsync_ReturnsUser()
        {
            // Arrange
            var dbContextOptions = DbContextHelper.GetOptions <WebApiCoreSeedContext>();
            var user             = GetSampleUser();

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                await dbContext.Users.AddAsync(user);

                await dbContext.SaveChangesAsync();
            }

            // Act
            User result;

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                var userService = new UserService(dbContext);
                result = await userService.GetByIdAsync(user.Id);
            }

            // Assert
            AssertUser(user, result);
        }
        public async Task UpdateAsync_ReturnsUpdatedUser()
        {
            // Arrange
            var dbContextOptions = DbContextHelper.GetOptions <WebApiCoreSeedContext>();
            var user             = GetSampleUser();

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                await dbContext.Users.AddAsync(user);

                await dbContext.SaveChangesAsync();
            }

            var toUpdate = new User
            {
                Id        = user.Id,
                FirstName = $"Updated {user.FirstName}",
                LastName  = $"Updated {user.LastName}",
                Email     = "*****@*****.**",
                UserName  = $"updated-{user.UserName}"
            };

            // Act
            User result;

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                var userService = new UserService(dbContext);
                result = await userService.UpdateAsync(toUpdate);
            }

            // Assert
            Assert.True(result.UpdatedOn > DateTime.Now.AddMinutes(-1));
            Assert.Equal(toUpdate.Email, result.Email);
            Assert.Equal(toUpdate.FirstName, result.FirstName);
            Assert.Equal(toUpdate.LastName, result.LastName);
            Assert.Equal(toUpdate.UserName, result.UserName);
        }
Beispiel #6
0
 public async Task <int> CreateAsync(User user)
 {
     user.CreatedOn = DateTime.Now;
     _dbContext.Users.Add(user);
     return(await _dbContext.SaveChangesAsync());
 }