public void TearDownObjects()
 {
     _userList = null;
     _dbContextMock = null;
     _userObj = null;
     _dbContextMock = null;
 }
        public void InitializeUnitTests()
        {
            // create some mock products to play with
            _userObj = new User
                                {
                                    Name = "Chris Smith",
                                    id = 2,
                                    Password = "******",
                                    Role = "Developer",
                                    IsEnabled = true
                                };

            _userList = new List<User>
                                 {
                                     _userObj,
                                     new User
                                         {
                                             Name = "Awin George",
                                             Password = "******",
                                             Role = "Developer",
                                             IsEnabled = false,
                                             id = 3
                                         },
                                     new User
                                         {
                                             Name = "Richard Child",
                                             Password = "******",
                                             Role = "Developer",
                                             IsEnabled = true,
                                             id = 4
                                         }
                                 };

            _mockUserRepository = new Mock<IUserRepository>();
        }
        public void InitializeUnitTests()
        {
            // create some mock products to play with
            _isValid = true;

            _userObj = new User
                           {
                               Name = "Chris Smith",
                               Password = "******",
                               Role = "Developer",
                               IsEnabled = true,
                               id = 2
                           };

            _userList =
                new List<User>
                    {
                        _userObj,
                        new User
                            {
                                Name = "Awin George",
                                Password = "******",
                                Role = "Developer",
                                IsEnabled = false,
                                id = 3
                            },
                        new User
                            {
                                Name = "Richard Child",
                                Password = "******",
                                Role = "Developer",
                                IsEnabled = true,
                                id = 4
                            }
                    }.AsQueryable();

            _dbContextMock = new Mock<dbContext>();

            _dbSetMock = new Mock<IDbSet<User>>();

            _dbSetMock.Setup(m => m.Provider).Returns(_userList.Provider);
            _dbSetMock.Setup(m => m.Expression).Returns(_userList.Expression);
            _dbSetMock.Setup(m => m.ElementType).Returns(_userList.ElementType);
            _dbSetMock.Setup(m => m.GetEnumerator()).Returns(_userList.GetEnumerator());
        }
        /// <summary>
        ///     Get user By Name
        /// </summary>
        /// <param name="name">user Name</param>
        /// <param name="userObj">Object to be retrieved</param>
        /// <returns>Returns true if success, else false</returns>
        public bool GetUserBy(string name, out User userObj)
        {
            var succeed = false;
            userObj = null;

            if (this._userRepository.GetUserBy(name, out userObj))
            {
                succeed = true;
            }

            return succeed;
        }
        /// <summary>
        ///     Get user By Id
        /// </summary>
        /// <param name="id">user Id</param>
        /// <param name="userObj">Object to be retrieved</param>
        /// <returns>Returns true if success, else false</returns>
        public bool GetUserBy(int id, out User userObj)
        {
            var succeed = false;
            userObj = null;

            if (this._userRepository.GetUserBy(id, out userObj)) succeed = true;

            return succeed;
        }
        /// <summary>
        ///     Get user By Id.
        /// </summary>
        /// <param name="id">user Id.</param>
        /// <param name="userObj">Object to be retrieved.</param>
        /// <returns>Returns true if success, else false.</returns>
        public bool GetUserBy(int id, out User userObj)
        {
            var succeed = false;

            userObj = this._context.User.First(p => p.id == id);
            succeed = true;

            return succeed;
        }
        /// <summary>
        ///     Get user By Name
        /// </summary>
        /// <param name="name">user Name</param>
        /// <param name="userObj">Object to be retrieved</param>
        /// <returns>Returns true if success, else false</returns>
        public bool GetUserBy(string name, out User userObj)
        {
            var succeed = false;

            userObj = this._context.User.First(p => p.Name == name);
            succeed = true;

            return succeed;
        }
 public void TearDownObjects()
 {
     _userList = null;
     _userObj = null;
     _mockUserRepository = null;
 }
        public void WhenUserNameAndPasswordIsProvidedValidateUserTest(
            [Values("Richard Child", "Chris Smith", "Awin George", "", null)] string userName,
            [Values("pass", "Test Password", "", null)] string password)
        {
            // return valid user
            var Valid = true;
            _userObj = _userList.FirstOrDefault(p => p.Name == userName && p.Password == password);
            _mockUserRepository.Setup(
                mr =>
                mr.ValidateUser(
                    It.IsIn(_userObj != null ? _userObj.Name : string.Empty),
                    It.IsIn(_userObj != null ? _userObj.Password : string.Empty),
                    out Valid)).Returns(true);

            // setup of Mock User Repository
            var target = new UserService(_mockUserRepository.Object);

            var success = target.ValidateUser(userName, password, out Valid);

            // assert
            if (success)
            {
                Assert.IsTrue(Valid);
            }
            else Assert.IsFalse(success);
        }
        public void WhenUserNameAndPasswordIsNullOrEmptyValidateUserTest(
            [Values("", null)] string userName,
            [Values("pass", "Test Password", "", null)] string password)
        {
            // return valid user
            var isValid = true;
            _userObj = _userList.FirstOrDefault(p => p.Name == userName && p.Password == password);
            _mockUserRepository.Setup(
                mr =>
                mr.ValidateUser(
                    It.IsIn(_userObj != null ? _userObj.Name : string.Empty),
                    It.IsIn(_userObj != null ? _userObj.Password : string.Empty),
                    out isValid)).Returns(false);

            // setup of Mock User Repository
            var target = new UserService(_mockUserRepository.Object);

            var success = target.ValidateUser(userName, password, out isValid);

            // assert
            Assert.IsFalse(success);
        }