Exemplo n.º 1
0
        public void GetAllUsers_MockDbSetUserAndContext_AndGetAllUsers()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            List <User>       usersCollection = DbSetTools.CreateUserList(1, 100, miniAutoFixture);
            IQueryable <User> users           = usersCollection.AsQueryable();
            var mockDbSet = new Mock <DbSet <User> >();

            mockDbSet.As <IQueryable <User> >().Setup(m => m.Provider).Returns(users.Provider);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.Expression).Returns(users.Expression);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.ElementType).Returns(users.ElementType);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());

            var mockContext = new Mock <OrganizerContext>();

            mockContext.Setup(c => c.User).Returns(mockDbSet.Object);
            var            logger         = new Mock <ILogger <ViewRepository> >();
            ViewRepository viewRepository = new ViewRepository(mockContext.Object, logger.Object);

            //action
            IEnumerable <UserView> userList = viewRepository.GetAllUsers();

            //assert
            Assert.Equal(users.Count(), userList.Count());
            users.ToList().ForEach(user =>
            {
                Assert.Equal
                (
                    userList.Count
                    (
                        userView =>
                        userView.Id == user.Id &&
                        userView.Name == user.Name &&
                        userView.Surname == user.Surname &&
                        userView.Deleted == user.Deleted

                    ),
                    1
                );
            }
                                   );
        }