Esempio n. 1
0
        public async Task BindModelAsync_ForParameter_UsesValidationFromParameter_WhenDerivedModelIsSet()
        {
            // Arrange
            var method              = GetType().GetMethod(nameof(TestMethodWithAttributes), BindingFlags.NonPublic | BindingFlags.Instance);
            var parameter           = method.GetParameters()[0];
            var parameterDescriptor = new ControllerParameterDescriptor
            {
                ParameterInfo = parameter,
                Name          = parameter.Name,
            };

            var actionContext         = GetControllerContext();
            var modelMetadataProvider = new TestModelMetadataProvider();

            var model = new DerivedPerson {
                DerivedProperty = "SomeValue"
            };
            var modelBindingResult = ModelBindingResult.Success(model);

            var parameterBinder = new ParameterBinder(
                modelMetadataProvider,
                Mock.Of <IModelBinderFactory>(),
                new DefaultObjectValidator(
                    modelMetadataProvider,
                    new[] { TestModelValidatorProvider.CreateDefaultProvider() },
                    new MvcOptions()),
                _optionsAccessor,
                NullLoggerFactory.Instance);

            var modelMetadata = modelMetadataProvider.GetMetadataForParameter(parameter);
            var modelBinder   = CreateMockModelBinder(modelBindingResult);

            // Act
            var result = await parameterBinder.BindModelAsync(
                actionContext,
                modelBinder,
                CreateMockValueProvider(),
                parameterDescriptor,
                modelMetadata,
                value : null);

            // Assert
            Assert.True(result.IsModelSet);
            Assert.Same(model, result.Model);

            Assert.False(actionContext.ModelState.IsValid);
            Assert.Collection(
                actionContext.ModelState,
                kvp =>
            {
                Assert.Equal(parameter.Name, kvp.Key);
                var error = Assert.Single(kvp.Value.Errors);
                Assert.Equal("Always Invalid", error.ErrorMessage);
            });
        }
Esempio n. 2
0
        public void InterfacePropertyTest()
        {
            IPerson person = new DerivedPerson();

            Assert.Equal("derived", person.Name);
        }
Esempio n. 3
0
        public void IsTypeWithDerivedClasses()
        {
            var d = new DerivedPerson();

            Assert.IsType <Person>(d);
        }
Esempio n. 4
0
        public void IsAssignableFrom()
        {
            var d = new DerivedPerson();

            Assert.IsAssignableFrom <Person>(d);
        }
 public void InterfacePropertyTest()
 {
     IPerson person = new DerivedPerson();
     Assert.AreEqual("derived", person.Name);
 }