public void DefaultRequiredClientValidationString()
        {
            var model = new MetadataTarget {LastName = "Arne", UserName = "******"};
            var metadataProvider = new DataAnnotationsModelMetadataProvider();
            var metadata = metadataProvider.GetMetadataForProperty(() => model, typeof (MetadataTarget), "UserName");

            var validator =
                _provider.GetValidators(metadata, new ControllerContext(new RequestContext(), new SomeController())).
                    Single();

            var clientRules = validator.GetClientValidationRules();

            Assert.True(validator.IsRequired);
            Assert.Equal("The UserName field is required.", clientRules.Single().ErrorMessage);
        }
        public void RequiredClientValidationString()
        {
            var model = new MetadataTarget {LastName = "Arne", UserName = "******"};
            var metadataProvider = new DataAnnotationsModelMetadataProvider();
            var metadata = metadataProvider.GetMetadataForProperty(() => model, typeof (MetadataTarget), "UserName");
            _stringProvider.Setup(k => k.GetValidationString(typeof (RequiredAttribute))).Returns(
                "Fältet '{0}' är humm!").Verifiable();

            var validator =
                _provider.GetValidators(metadata, new ControllerContext(new RequestContext(), new SomeController())).
                    Single();

            var clientRules = validator.GetClientValidationRules();

            Assert.True(validator.IsRequired);
            Assert.Equal("Fältet 'UserName' är humm!", clientRules.Single().ErrorMessage);
            _stringProvider.VerifyAll();
        }
        public void TestNotTranslatedProperty()
        {
            DefaultUICulture.Reset();
            var stringProvider = new Mock<ILocalizedStringProvider>();
            var provider = new LocalizedModelMetadataProvider(stringProvider.Object);
            stringProvider.Setup(
                k =>
                k.GetModelString(It.Is<Type>(t => t == typeof (MetadataTarget)), It.Is<string>(t => t == "UserName"))).
                Returns((string) null).Verifiable();

            var subject = new MetadataTarget
                              {
                                  LastName = "Arne",
                                  UserName = "******"
                              };

            var actual = provider.GetMetadataForProperty(() => subject, typeof (MetadataTarget), "UserName");

            Assert.Equal(null, actual.DisplayName);
        }
        public void TestAnotherProperty()
        {
            var stringProvider = new Mock<ILocalizedStringProvider>();
            var provider = new LocalizedModelMetadataProvider(stringProvider.Object);
            stringProvider.Setup(
                k =>
                k.GetModelString(It.Is<Type>(t => t == typeof (MetadataTarget)), It.Is<string>(t => t == "LastName"))).
                Returns("Efternamn").Verifiable();

            var subject = new MetadataTarget
                              {
                                  LastName = "Arne",
                                  UserName = "******"
                              };

            var actual = provider.GetMetadataForProperty(() => subject, typeof (MetadataTarget), "LastName");

            Assert.Equal("Efternamn", actual.DisplayName);
            stringProvider.VerifyAll();
        }