title: Using a custom ModelMetadataProvider in a unit test
body: <p>Pretty new to MVC so hopefully this is a simple question.</p>
  
  <p>I have written a custom binding attribute that requires access to the <code>httpContext</code>. In order to inject a mock <code>httpContext</code> during unit tests, I have written an <code>InjectingMetadataProvider</code> that populates the <code>Context</code> property on any of my custom attributes.</p>
  
  <p>I have managed to get this to work in the following test:</p>
  
  <pre><code>[TestMethod]
  public void Marker_ShouldBind_Id()
  {
      // Arrange
      var formCollection = new NameValueCollection 
      { 
          { "id", "2" }
      };
  
      var context = new Mock&lt;HttpContextBase&gt;();
      context.Setup(c =&gt; c.User).Returns((IPrincipal)null); 
  
      var metaProvider = new InjectingMetadataProvider(context.Object);
      ModelMetadataProviders.Current = metaProvider;  //why do I need this?
  
      var bindingContext = new ModelBindingContext
      {
          ModelName     = string.Empty,
          ValueProvider = new NameValueCollectionValueProvider(formCollection, null),
          ModelMetadata = metaProvider.GetMetadataForType(null, typeof(Marker)),
      };
  
      var binder = new DefaultModelBinder();
  
      // Act
      var marker = (Marker)binder.BindModel(new ControllerContext(), bindingContext);
  
      // Assert
      marker.Id.Should().Be(2);
  }
  </code></pre>
  
  <p>However, if I comment out the line that sets my <code>InjectingMetadataProvider</code> to <code>ModelMetadataProviders.Current</code>, then my <code>InjectingMetadataProvider.CreateMetadata()</code> override gets handed a blank list of attributes, and so the test fails because my custom attributes don't get their context set.</p>
  
  <p>Why do I need to set it to <code>Current</code> when I'm using it explicitly anyway? I don't want to be setting static stuff in my tests.</p>
  
  <p>I may be doing something stupid because I'm feeling in the dark a bit at the moment due to my unfamiliarity with the framework.</p>