Repository: dotnet/aspnetcore
File: src/Mvc/Mvc.Core/test/ModelBinding/Metadata/DefaultModelMetadataProviderTest.cs (C#)

Code before:

        // Assert
        foreach (var property in metadata)
        {
            Assert.NotNull(property.PropertyGetter);
            Assert.Null(property.PropertyGetter(null));
        }
    }

    [Fact]
    public void GetMetadataForParameter_SuppliesEmptyAttributes_WhenParameterHasNoAttributes()
    {
        // Arrange
        var provider = CreateProvider();
        var parameters = typeof(ModelType)
            .GetMethod(nameof(ModelType.Method1))
            .GetParameters();

        // Act
        var metadata = provider.GetMetadataForParameter(parameters[0]);

        // Assert
        var defaultMetadata = Assert.IsType<DefaultModelMetadata>(metadata);

        // Not exactly "no attributes" due to pseudo-attributes on object.
        // After CoreCLR consistency fix, object.GetCustomAttributes() returns all pseudo-attributes.
        Assert.Equal(5, defaultMetadata.Attributes.Attributes.Count);
        Assert.Contains(defaultMetadata.Attributes.Attributes, attr => attr is SerializableAttribute);
        Assert.Contains(defaultMetadata.Attributes.Attributes, attr => attr.GetType().Name == "NullableContextAttribute");
        Assert.Contains(defaultMetadata.Attributes.Attributes, attr => attr.GetType().Name == "ClassInterfaceAttribute");
        Assert.Contains(defaultMetadata.Attributes.Attributes, attr => attr.GetType().Name == "ComVisibleAttribute");
        Assert.Contains(defaultMetadata.Attributes.Attributes, attr => attr.GetType().Name == "TypeForwardedFromAttribute");
    }

    [Fact]
    public void GetMetadataForParameter_SuppliesAttributes_WhenParamHasAttributes()
    {
        // Arrange
        var provider = CreateProvider();
        var parameters = typeof(ModelType)
            .GetMethod(nameof(ModelType.Method1))
            .GetParameters();

        // Act
        var metadata = provider.GetMetadataForParameter(parameters[1]);

        // Assert
        var defaultMetadata = Assert.IsType<DefaultModelMetadata>(metadata);
        Assert.Collection(
            // Take(2) to ignore SerializableAttribute on object.
            defaultMetadata.Attributes.Attributes.Take(2),

Review comment (suggestion):
Should this test be deleted instead? It does not make sense for ASP.NET to test internal implementation details of CoreCLR.

Code after:

        // Assert
        foreach (var property in metadata)
        {
            Assert.NotNull(property.PropertyGetter);
            Assert.Null(property.PropertyGetter(null));
        }
    }

    [Fact]
    public void GetMetadataForParameter_SuppliesEmptyAttributes_WhenParameterHasNoAttributes()
    {
        // Arrange
        var provider = CreateProvider();
        var parameters = typeof(ModelType)
            .GetMethod(nameof(ModelType.Method1))
            .GetParameters();

        // Act
        var metadata = provider.GetMetadataForParameter(parameters[0]);

        // Assert
        var defaultMetadata = Assert.IsType<DefaultModelMetadata>(metadata);

        // The parameter itself has no attributes
        Assert.Empty(defaultMetadata.Attributes.ParameterAttributes);

        // Type attributes exist (but we don't care what they are - that's runtime implementation detail)
        Assert.NotEmpty(defaultMetadata.Attributes.TypeAttributes);

        // Combined attributes = ParameterAttributes + TypeAttributes (when parameter has no attributes)
        Assert.Equal(defaultMetadata.Attributes.TypeAttributes, defaultMetadata.Attributes.Attributes);
    }

    [Fact]
    public void GetMetadataForParameter_SuppliesAttributes_WhenParamHasAttributes()
    {
        // Arrange
        var provider = CreateProvider();
        var parameters = typeof(ModelType)
            .GetMethod(nameof(ModelType.Method1))
            .GetParameters();

        // Act
        var metadata = provider.GetMetadataForParameter(parameters[1]);

        // Assert
        var defaultMetadata = Assert.IsType<DefaultModelMetadata>(metadata);
        Assert.Collection(
            // Take(2) to ignore SerializableAttribute on object.
            defaultMetadata.Attributes.Attributes.Take(2),