public static Mock <IUserRepository> GetMockAndSetUpAsync(IEnumerable <User> defaultUserList = null) { var repositoryMock = GetMock(); var users = defaultUserList ?? UserDummyGenerator.GenerateCollection(5); repositoryMock.Setup(repository => repository.GetAllAsync()).ReturnsAsync(() => users); return(repositoryMock); }
public async Task UserRepositoryEFTest_Add() { var user = UserDummyGenerator.Generate(); var mockDbSet = new Mock <DbSet <User> >(); var mockDbContext = DbContextMock <DbContext> .GetMockAndSetUpDbSet(mockDbSet); var mockUnitOfWork = UnitOfWorkMock.GetMockAndSetUpContext(mockDbContext); var repositoryUser = new UserRepositoryEF(mockUnitOfWork.Object); await repositoryUser.AddAsync(user); mockDbSet.Verify(m => m.AddAsync(It.Is <User>(y => y == user), It.IsAny <System.Threading.CancellationToken>()), Times.Once()); }
public async Task UserRepositoryEFTest_GetAll() { var users = UserDummyGenerator.GenerateCollection(5); var mockDbSet = DbSetMock <User> .GetMockAndSetUpQueryble(users); var mockDbContext = DbContextMock <DbContext> .GetMockAndSetUpDbSet(mockDbSet); var mockUnitOfWork = UnitOfWorkMock.GetMockAndSetUpContext(mockDbContext); var userRepositoryEF = new UserRepositoryEF(mockUnitOfWork.Object); var resultUsers = await userRepositoryEF.GetAllAsync(); Assert.True(Enumerable.SequenceEqual(users.OrderBy(t => t.UserId), resultUsers.OrderBy(t => t.UserId), new UserComparer())); }
public async Task UsersControllerTests_CreateUser() { var user = UserDummyGenerator.Generate(); var userRepository = UserRepositoryMock.GetMock(); var unitOfWork = UnitOfWorkMock.GetMock(); var userController = new UsersController(unitOfWork.Object, userRepository.Object); var result = await userController.CreateUser(user); Assert.NotNull(result); Assert.IsType <OkResult>(result); userRepository.Verify(x => x.AddAsync(It.Is <User>(y => y == user))); unitOfWork.Verify(x => x.CommitAsync()); }
public async Task UsersControllerTests_GetAll() { var users = UserDummyGenerator.GenerateCollection(5) as List <User>; var userRepository = UserRepositoryMock.GetMockAndSetUpAsync(users); var unitOfWork = UnitOfWorkMock.GetMock(); var userController = new UsersController(unitOfWork.Object, userRepository.Object); var result = await userController.GetAll(); Assert.NotNull(result); Assert.IsType <OkObjectResult>(result); var resultUsers = (result as OkObjectResult).Value as List <User>; Assert.True(Enumerable.SequenceEqual(users.OrderBy(t => t.UserId), resultUsers.OrderBy(t => t.UserId), new UserComparer())); }