Пример #1
0
        public void GetByIdGotDataFromDao()
        {
            const int userId = 1;

            Internship.Cashe.Models.User user          = new Internship.Cashe.Models.User();
            Mock <IUserCache>            userCacheMock = new Mock <IUserCache>();

            userCacheMock.Setup(x => x.GetUserById(userId))
            .Verifiable();
            Mock <IUserDAO> userDaoMock = new Mock <IUserDAO>();

            userDaoMock.Setup(x => x.GetById(userId))
            .Returns(user)
            .Verifiable();
            userCacheMock.Setup(x => x.Set(user))
            .Verifiable();

            IUserDAO userDao = new CachedUserDAO(userDaoMock.Object, userCacheMock.Object);

            Internship.Cashe.Models.User result = userDao.GetById(userId);
            Assert.That(result, Is.Not.Null);
            userCacheMock.Verify(x => x.GetUserById(userId), Times.Once);
            userDaoMock.Verify(x => x.GetById(userId), Times.Once);
            userCacheMock.Verify(x => x.Set(user), Times.Once);
        }
        public void GetByIdGotDataFromCache()
        {
            const int         userId        = 1;
            Mock <IUserCache> userCacheMock = new Mock <IUserCache>();

            userCacheMock.Setup(x => x.GetUserById(userId))
            .Returns(new User())
            .Verifiable();

            Mock <IUserDAO> userDaoMock = new Mock <IUserDAO>();

            userDaoMock.Setup(x => x.GetById(userId))
            .Verifiable();

            IUserDAO userDao = new CachedUserDAO(userDaoMock.Object, userCacheMock.Object);
            User     user    = userDao.GetById(userId);

            Assert.That(user, Is.Not.Null);
            userCacheMock.Verify(x => x.GetUserById(userId), Times.Once);
            userDaoMock.Verify(x => x.GetById(userId), Times.Never);
        }