示例#1
0
        public void GetUser_MockDbSetUserAndContext_AndGetUser()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            User        userSpecified   = DbSetTools.CreateUser(101, miniAutoFixture);
            List <User> usersCollection =
                DbSetTools.CreateUserList(1, 100, miniAutoFixture)
                .Concat(new User[] { userSpecified }).ToList();


            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
            UserView userView = viewRepository.GetUser(userSpecified.Id);

            //assert
            Assert.Equal(userSpecified.Id, userView.Id);
            Assert.Equal(userSpecified.Name, userView.Name);
            Assert.Equal(userSpecified.Surname, userView.Surname);
            Assert.Equal(userSpecified.Deleted, userView.Deleted);
        }