public void SetModelCallsGetMetadataForTypeExactlyOnce()
        {
            // Arrange
            var metadataProvider = new Mock <IModelMetadataProvider>(MockBehavior.Strict);

            metadataProvider
            .Setup(m => m.GetMetadataForType(It.IsAny <Func <object> >(), typeof(object)))
            .Returns(new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(object)))
            .Verifiable();
            metadataProvider
            .Setup(m => m.GetMetadataForType(It.IsAny <Func <object> >(), typeof(TestModel)))
            .Returns(new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(TestModel)))
            .Verifiable();
            var modelState = new ModelStateDictionary();
            var viewData   = new TestViewDataDictionary(metadataProvider.Object, modelState);
            var model      = new TestModel();

            // Act
            viewData.SetModelPublic(model);

            // Assert
            Assert.NotNull(viewData.ModelMetadata);
            // Verifies if the GetMetadataForType is called only once.
            metadataProvider.Verify(
                m => m.GetMetadataForType(It.IsAny <Func <object> >(), typeof(object)), Times.Once());
            // Verifies if GetMetadataForProperties and GetMetadataForProperty is not called.
            metadataProvider.Verify(
                m => m.GetMetadataForProperties(It.IsAny <Func <object> >(), typeof(object)), Times.Never());
            metadataProvider.Verify(
                m => m.GetMetadataForProperty(
                    It.IsAny <Func <object> >(), typeof(object), It.IsAny <string>()), Times.Never());
        }
Beispiel #2
0
    public void SetModel_GetsNewModelMetadata_IfSourceTypeIsObject()
    {
        // Arrange
        var metadataProvider = new Mock <IModelMetadataProvider>(MockBehavior.Strict);

        metadataProvider
        .Setup(m => m.GetMetadataForType(typeof(object)))
        .Returns(new EmptyModelMetadataProvider().GetMetadataForType(typeof(object)))
        .Verifiable();
        metadataProvider
        .Setup(m => m.GetMetadataForType(typeof(TestModel)))
        .Returns(new EmptyModelMetadataProvider().GetMetadataForType(typeof(TestModel)))
        .Verifiable();
        var viewData = new TestViewDataDictionary(metadataProvider.Object);
        var model    = new TestModel();

        // Act
        viewData.SetModelPublic(model);

        // Assert
        Assert.NotNull(viewData.ModelMetadata);

        // For the constructor.
        metadataProvider.Verify(m => m.GetMetadataForType(typeof(object)), Times.Once());

        // For SetModel().
        metadataProvider.Verify(m => m.GetMetadataForType(typeof(TestModel)), Times.Once());
    }
Beispiel #3
0
    public void ModelSetter_SetNonNullableToNull_Throws()
    {
        // Arrange
        var viewData = new TestViewDataDictionary(new EmptyModelMetadataProvider(), typeof(int));

        // Act & Assert
        var exception = Assert.Throws <InvalidOperationException>(() => viewData.SetModelPublic(value: null));

        Assert.Equal(
            "The model item passed is null, but this ViewDataDictionary instance requires a non-null model item " +
            $"of type '{ typeof(int) }'.",
            exception.Message);
    }
Beispiel #4
0
    public void SetModel_Throws_IfModelIncompatibleWithDeclaredType(object model, Type expectedType)
    {
        // Arrange
        var viewData = new TestViewDataDictionary(new EmptyModelMetadataProvider(), typeof(TestModel));

        // Act & Assert
        var exception = Assert.Throws <InvalidOperationException>(() => viewData.SetModelPublic(model));

        Assert.Equal(
            $"The model item passed into the ViewDataDictionary is of type '{ model.GetType() }', but this " +
            $"ViewDataDictionary instance requires a model item of type '{ typeof(TestModel) }'.",
            exception.Message);
    }
Beispiel #5
0
    public void SetModel_DoesNotGetNewModelMetadata_IfTypeCompatible()
    {
        // Arrange
        var metadataProvider = new Mock <IModelMetadataProvider>(MockBehavior.Strict);

        metadataProvider
        .Setup(m => m.GetMetadataForType(typeof(TestModel)))
        .Returns(new EmptyModelMetadataProvider().GetMetadataForType(typeof(TestModel)))
        .Verifiable();
        var viewData = new TestViewDataDictionary(metadataProvider.Object, typeof(TestModel));
        var model    = new TestModel();

        // Act
        viewData.SetModelPublic(model);

        // Assert
        Assert.NotNull(viewData.ModelMetadata);
        metadataProvider.Verify(m => m.GetMetadataForType(typeof(TestModel)), Times.Once());
    }
Beispiel #6
0
        public void SetModelUsesPassedInModelMetadataProvider()
        {
            // Arrange
            var metadataProvider = new Mock <IModelMetadataProvider>();

            metadataProvider.Setup(m => m.GetMetadataForType(It.IsAny <Func <object> >(), typeof(TestModel)))
            .Returns(new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(TestModel)))
            .Verifiable();
            var modelState = new ModelStateDictionary();
            var viewData   = new TestViewDataDictionary(metadataProvider.Object, modelState);
            var model      = new TestModel();

            // Act
            viewData.SetModelPublic(model);

            // Assert
            Assert.NotNull(viewData.ModelMetadata);
            metadataProvider.Verify();
        }
        public void SetModelUsesPassedInModelMetadataProvider()
        {
            // Arrange
            var metadataProvider = new Mock<IModelMetadataProvider>();
            metadataProvider
                .Setup(m => m.GetMetadataForType(typeof(object)))
                .Returns(new EmptyModelMetadataProvider().GetMetadataForType(typeof(object)))
                .Verifiable();
            metadataProvider
                .Setup(m => m.GetMetadataForType(typeof(TestModel)))
                .Returns(new EmptyModelMetadataProvider().GetMetadataForType(typeof(TestModel)))
                .Verifiable();
            var modelState = new ModelStateDictionary();
            var viewData = new TestViewDataDictionary(metadataProvider.Object, modelState);
            var model = new TestModel();

            // Act
            viewData.SetModelPublic(model);

            // Assert
            Assert.NotNull(viewData.ModelMetadata);
            metadataProvider.Verify();
        }
Beispiel #8
0
    public void ModelSetter_DifferentType_UpdatesModelMetadata(Type originalMetadataType)
    {
        // Arrange
        var metadataProvider = new EmptyModelMetadataProvider();
        var metadata         = metadataProvider.GetMetadataForType(originalMetadataType);
        var explorer         = new ModelExplorer(metadataProvider, metadata, model: null);
        var viewData         = new TestViewDataDictionary(metadataProvider)
        {
            ModelExplorer = explorer,
        };

        // Act
        viewData.Model = true;

        // Assert
        Assert.NotNull(viewData.ModelExplorer);
        Assert.NotNull(viewData.ModelMetadata);
        Assert.NotSame(explorer, viewData.ModelExplorer);
        Assert.Equal(typeof(bool), viewData.ModelMetadata.ModelType);

        var model = Assert.IsType <bool>(viewData.Model);

        Assert.True(model);
    }
        public void SetModelCallsGetMetadataForTypeExactlyOnce()
        {
            // Arrange
            var metadataProvider = new Mock<IModelMetadataProvider>(MockBehavior.Strict);
            metadataProvider
                .Setup(m => m.GetMetadataForType(typeof(object)))
                .Returns(new EmptyModelMetadataProvider().GetMetadataForType(typeof(object)))
                .Verifiable();
            metadataProvider
                .Setup(m => m.GetMetadataForType(typeof(TestModel)))
                .Returns(new EmptyModelMetadataProvider().GetMetadataForType(typeof(TestModel)))
                .Verifiable();
            var modelState = new ModelStateDictionary();
            var viewData = new TestViewDataDictionary(metadataProvider.Object, modelState);
            var model = new TestModel();

            // Act
            viewData.SetModelPublic(model);

            // Assert
            Assert.NotNull(viewData.ModelMetadata);
            // Verifies if the GetMetadataForType is called only once.
            metadataProvider.Verify(
                m => m.GetMetadataForType(typeof(object)), Times.Once());
            // Verifies if GetMetadataForProperties and GetMetadataForProperty is not called.
            metadataProvider.Verify(
                m => m.GetMetadataForProperties(typeof(object)), Times.Never());
        }