Inheritance: System.ComponentModel.DataAnnotations.ValidationAttribute
        public void ShouldReturnFalseWhenParameterIsNull()
        {
            var attr = new ValidateUsernameAttribute { UsersResource = _userResource.Object };
            var result = attr.IsValid(null);

            Assert.IsFalse(result);
            Assert.AreEqual("Model property is null/empty.", attr.ErrorMessage);
        }
        public void ShouldReturnFalseWhenUserErrorIsNull()
        {
            _userResource.Setup(a => a.GetByUserName(It.IsAny<string>()))
                .Returns(new User());

            var attr = new ValidateUsernameAttribute { UsersResource = _userResource.Object };
            var result = attr.IsValid("foo");

            Assert.IsFalse(result);
        }
        public void ShouldReturnTrueWhenUsernameIsValid()
        {
            _userResource.Setup(a => a.GetByUserName(It.IsAny<string>()))
                .Returns(new User { Error = new Error { Id = (int)Utils.Constants.Error.RecordNotFound } });

            var attr = new ValidateUsernameAttribute { UsersResource = _userResource.Object };
            var result = attr.IsValid("foo");

            Assert.IsTrue(result);
        }
        public void ShouldReturnFalseWhenUserErrorIdIsNotRecordNotFound()
        {
            _userResource.Setup(a => a.GetByUserName(It.IsAny<string>()))
                .Returns(new User { Error = new Error { Id = (int)Utils.Constants.Error.InternalError } });

            var attr = new ValidateUsernameAttribute { UsersResource = _userResource.Object };
            var result = attr.IsValid("foo");

            Assert.IsFalse(result);
            Assert.AreEqual("Username is already in use", attr.ErrorMessage);
        }
        public void ShouldReturnFalseWhenUsernameIsNotString()
        {
            var attr = new ValidateUsernameAttribute { UsersResource = _userResource.Object };
            var result =  attr.IsValid(13);

            Assert.IsFalse(result);
            Assert.AreEqual("Model property is not a string.", attr.ErrorMessage);
        }
        public void ShouldReturnFalseWhenFetchingUserFails()
        {
            _userResource.Setup(a => a.GetByUserName(It.IsAny<string>()))
                .Throws(new Exception());

            var attr = new ValidateUsernameAttribute { UsersResource = _userResource.Object };
            var result = Assert.Throws<BlogException>(() => attr.IsValid("foo"));

            Assert.IsInstanceOf(typeof(BlogException), result);
        }