public async Task Call_Create_ShouldCall_CreateOnDbContext() { // Arrange var user = new DomainUser(); var autoIncrementedUserId = 1; FakeDbContextFactory.CreateMockCollection(_dbContextMock.Setup(db => db.Users)); FakeDbContextFactory.CreateMockCollection(_dbContextMock.Setup(db => db.Sequences), new[] { new Sequence { SequenceValue = 1 } }); // Act var userId = await _userRepository.Create(user); // Assert _dbContextMock.Verify(db => db.Users.InsertOneAsync(It.IsAny <User>(), It.IsAny <InsertOneOptions>(), default), Times.Once); Assert.Equal(autoIncrementedUserId, userId); }
public async Task Call_Get_ShouldCall_FindAsyncOnDbContext_And_ReturnAUser() { // Arrange var userId = 1; var users = new List <User> { new User { Id = userId, Name = "John" }, }; FakeDbContextFactory.CreateMockCollection(_dbContextMock.Setup(db => db.Users), users); // Act var user = await _userRepository.Get(userId); // Assert Assert.NotNull(user); _dbContextMock.Verify(db => db.Users.FindAsync <User>(It.IsAny <FilterDefinition <User> >(), null, default), Times.Once); }
public async Task Call_GetAll_ShouldCall_FindAsyncOnDbContext_And_ReturnPagedUsersList(int pageSize, int pageIndex, int expcetedPagedUserCount) { // Arrange var pageInfo = new PageInfo(pageSize, pageIndex); var users = new List <User> { new User { Id = 1, Name = "John" }, new User { Id = 2, Name = "John" }, new User { Id = 3, Name = "John" }, new User { Id = 4, Name = "John" }, new User { Id = 5, Name = "John" } }; var findOptions = new FindOptions <User, User> { Skip = pageInfo.CalculateSkip(), Limit = pageInfo.PageSize }; FakeDbContextFactory.CreateMockCollection(_dbContextMock.Setup(db => db.Users), users, findOptions); // Act var pagedUsers = await _userRepository.GetAll(pageInfo, CancellationToken.None); // Assert Assert.Equal(expcetedPagedUserCount, pagedUsers.Result.Count()); Assert.Equal(pageSize, pagedUsers.PageInfo.PageSize); Assert.Equal(pageIndex, pagedUsers.PageInfo.PageIndex); Assert.Equal(users.Count(), pagedUsers.PageInfo.TotalCount); _dbContextMock.Verify(db => db.Users.FindAsync(FilterDefinition <User> .Empty, It.IsAny <FindOptions <User, User> >(), It.IsAny <CancellationToken>()), Times.Once); }