public void BoardRepositoryCanCreateBoard() { /* Begin Arrange */ var data = my_list.AsQueryable(); mock_boards.As<IQueryable<Board>>().Setup(m => m.Provider).Returns(data.Provider); mock_boards.As<IQueryable<Board>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); mock_boards.As<IQueryable<Board>>().Setup(m => m.ElementType).Returns(data.ElementType); mock_boards.As<IQueryable<Board>>().Setup(m => m.Expression).Returns(data.Expression); // This allows BoardRepository to call Boards.Add and have it update the my_list instance and Enumerator // Connect DbSet.Add to List.Add so they work together mock_boards.Setup(m => m.Add(It.IsAny<Board>())).Callback((Board b) => my_list.Add(b)); mock_context.Setup(m => m.Boards).Returns(mock_boards.Object); BoardRepository board_repo = new BoardRepository(mock_context.Object); string title = "My Awesome Board"; /* End Arrange */ /* Begin Act */ Board added_board = board_repo.CreateBoard(title, owner); /* End Act */ /* Begin Assert */ Assert.IsNotNull(added_board); mock_boards.Verify(m => m.Add(It.IsAny<Board>())); mock_context.Verify(x => x.SaveChanges(), Times.Once()); Assert.AreEqual(1, board_repo.GetBoardCount()); /* End Assert */ }
public void BoardRepositoryCanGetBoardCount() { /* Begin Arrange */ var data = my_list.AsQueryable(); //mock_boards.Object.Add(new Board { Title = "My Awesome Board", Owner = new ApplicationUser() }); //var data = mock_boards.Object.AsQueryable(); mock_boards.As<IQueryable<Board>>().Setup(m => m.Provider).Returns(data.Provider); mock_boards.As<IQueryable<Board>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); mock_boards.As<IQueryable<Board>>().Setup(m => m.ElementType).Returns(data.ElementType); mock_boards.As<IQueryable<Board>>().Setup(m => m.Expression).Returns(data.Expression); mock_context.Setup(m => m.Boards).Returns(mock_boards.Object); //mock_context.Object.SaveChanges(); // This saves something to the Database BoardRepository board_repository = new BoardRepository(mock_context.Object); /* End Arrange */ /* Begin Act */ int actual = board_repository.GetBoardCount(); /* End Act */ /* Begin Assert */ Assert.AreEqual(0, actual); /* End Assert */ /* Begin Act */ my_list.Add(new Board { Title = "My Awesome Board" }); mock_boards.As<IQueryable<Board>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); /* End Act */ /* Begin Assert */ Assert.AreEqual(1, board_repository.GetBoardCount()); /* End Assert */ }