public void RegisterValidatableObjectAdapterGuardClauses()
        {
            // Attribute type cannot be null
            ExceptionHelper.ExpectArgumentNullException(
                () => DataAnnotations4ModelValidatorProvider.RegisterValidatableObjectAdapter(null, typeof(MyValidatableAdapter)),
                "modelType");

            // Adapter type cannot be null
            ExceptionHelper.ExpectArgumentNullException(
                () => DataAnnotations4ModelValidatorProvider.RegisterValidatableObjectAdapter(typeof(MyValidatableClass), null),
                "adapterType");

            // Validation attribute must derive from ValidationAttribute
            ExceptionHelper.ExpectArgumentException(
                () => DataAnnotations4ModelValidatorProvider.RegisterValidatableObjectAdapter(typeof(object), typeof(MyValidatableAdapter)),
                "The type System.Object must derive from System.ComponentModel.DataAnnotations.IValidatableObject.\r\nParameter name: modelType");

            // Adapter must derive from ModelValidator
            ExceptionHelper.ExpectArgumentException(
                () => DataAnnotations4ModelValidatorProvider.RegisterValidatableObjectAdapter(typeof(MyValidatableClass), typeof(object)),
                "The type System.Object must derive from System.Web.Mvc.ModelValidator.\r\nParameter name: adapterType");

            // Adapter must have the expected constructor
            ExceptionHelper.ExpectArgumentException(
                () => DataAnnotations4ModelValidatorProvider.RegisterValidatableObjectAdapter(typeof(MyValidatableClass), typeof(MyValidatableAdapterBadCtor)),
                "The type Microsoft.Web.Mvc.AspNet4.Test.DataAnnotations4ModelValidatorProviderTest+MyValidatableAdapterBadCtor must have a public constructor which accepts two parameters of types System.Web.Mvc.ModelMetadata and System.Web.Mvc.ControllerContext.\r\nParameter name: adapterType");
        }
        public void RegisterValidatableObjectAdapter()
        {
            var oldFactories = DataAnnotations4ModelValidatorProvider.ValidatableFactories;

            try {
                // Arrange
                DataAnnotations4ModelValidatorProvider.ValidatableFactories = new Dictionary <Type, DataAnnotations4ValidatableObjectAdapterFactory>();
                IValidatableObject validatable = new Mock <IValidatableObject>().Object;

                // Act
                DataAnnotations4ModelValidatorProvider.RegisterValidatableObjectAdapter(validatable.GetType(), typeof(MyValidatableAdapter));

                // Assert
                var type = DataAnnotations4ModelValidatorProvider.ValidatableFactories.Keys.Single();
                Assert.AreEqual(validatable.GetType(), type);

                var factory   = DataAnnotations4ModelValidatorProvider.ValidatableFactories.Values.Single();
                var metadata  = ModelMetadataProviders.Current.GetMetadataForType(() => null, typeof(object));
                var context   = new ControllerContext();
                var validator = factory(metadata, context);
                Assert.AreEqual(typeof(MyValidatableAdapter), validator.GetType());
            }
            finally {
                DataAnnotations4ModelValidatorProvider.ValidatableFactories = oldFactories;
            }
        }