コード例 #1
0
        public void InsertUser_MockDbSetUser_AndGetInsertUser()
        {
            //assign
            int             newIdUser       = 11;
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            List <User>       usersCollection = DbSetTools.CreateUserList(1, 10, 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());
            mockDbSet.Setup(m => m.Add(It.IsAny <User>()))
            .Callback <User>(a =>
            {
                a.Id = newIdUser;
                usersCollection.Add(a);
            });

            var mockContext = new Mock <OrganizerContext>();

            mockContext.Setup(c => c.User).Returns(mockDbSet.Object);
            var logger = new Mock <ILogger <ModifyRepository> >();
            ModifyRepository modifyRepository = new ModifyRepository(mockContext.Object, logger.Object);
            UserModify       userModify       = DomainTools.CreateUserModify(miniAutoFixture);

            //action
            UserView userView = modifyRepository.InsertUser(userModify);

            //assert
            mockDbSet.Verify
            (
                a => a.Add
                (
                    It.Is <User>
                    (

                        user => user.Name == userModify.Name &&
                        user.Surname == userModify.Surname &&
                        !user.Deleted &&
                        user.Id == newIdUser
                    )
                ),
                Times.Once
            );
            mockContext.Verify(a => a.SaveChanges(), Times.Once);


            Assert.Equal(userModify.Name, userView.Name);
            Assert.Equal(userModify.Surname, userView.Surname);
            Assert.Equal(false, userView.Deleted);
            Assert.Equal(newIdUser, userView.Id);
        }
コード例 #2
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
                );
            }
                                   );
        }
コード例 #3
0
        public void ModifytUser_MockDbSetUser_AndGetUpdateUser()
        {
            //assign
            int             idUserUpdate    = 1;
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            List <User>       usersCollection = DbSetTools.CreateUserList(1, 10, miniAutoFixture);
            IQueryable <User> users           = usersCollection.AsQueryable();
            UserModify        userModify      = DomainTools.CreateUserModify(miniAutoFixture);

            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 <ModifyRepository> >();
            ModifyRepository modifyRepository = new ModifyRepository(mockContext.Object, logger.Object);


            //action
            UserView userView = modifyRepository.UpdateUser(idUserUpdate, userModify);

            //assert
            User userModified = usersCollection.Where(a => a.Id == idUserUpdate).Single();

            mockContext.Verify(a => a.Attach(It.Is <User>(user => userModified == user)), Times.Once);
            mockContext.Verify(a => a.SaveChanges(), Times.Once);
            Assert.Equal(userModify.Name, userView.Name);
            Assert.Equal(userModify.Surname, userView.Surname);
            Assert.Equal(userModified.Deleted, userView.Deleted);
            Assert.Equal(idUserUpdate, userView.Id);
        }
コード例 #4
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);
        }