title: Trying to Test a Controller with a UrlHelper
body: <p>Trying to create a <code>URLHelper</code> for testing purposes throws a <code>NullReferenceException</code>.</p>
  
  <p>Example:</p>
  
  <pre><code>[Fact]
  public async void AuthenticateAsyncTest()
  {
    // Arrange
    var controller = new Controller(serviceProvider)
    {
      Url = new UrlHelper(new ActionContext()) // Exception thrown
    };
  
    // Act
    var result = await controller.Authenticate() as ViewResult;
  
    // Assert
    Assert.NotNull(result);
  }
  </code></pre>
  
  <p>Every time I run this Test, the Exception that is thrown in <code>Url = new UrlHelper(new ActionContext())</code> is:</p>
  
  <p>Exception.Message:</p>
  
  <blockquote>
    <p>Message: System.NullReferenceException : Object reference not set to
    an instance of an object.</p>
  </blockquote>
  
  <p>Exception.StackTrace:</p>
  
  <blockquote>
    <p>UrlHelperBase.ctor(ActionContext actionContext)
    ControllerUnitTest.AuthenticateAsyncTest()</p>
  </blockquote>
  
  <p>Using:</p>
  
  <blockquote>
    <p>xUnit 2.4.1, 
    Microsoft.NETCore.App 2.2.0,
    Microsoft.AspNetCore.Routing.Abstractions 2.2.0</p>
  </blockquote>
  
  <p>To recreate the Exception:</p>
  
  <ol>
  <li>Create a empty MVC core 2.2 solution</li>
  <li>Create a xunit test Project</li>
  <li>Install the NuGet Microsoft.AspNetCore.Mvc.Core 2.2.0</li>
  <li>Write in the test: var Url = new UrlHelper(new ActionContext());</li>
  <li>Run test</li>
  </ol>
  
  <p>Should look like this:</p>
  
  <pre><code>using Microsoft.AspNetCore.Mvc;
  using Microsoft.AspNetCore.Mvc.Routing;
  using Xunit;
  
  namespace XUnitTestProject1
  {
      public class UnitTest1
      {
          [Fact]
          public void Test1()
          {
              var Url = new UrlHelper(new ActionContext());
          }
      }
  }
  </code></pre>
  
  <p>My questions:</p>
  
  <ol>
  <li>Is there a bug, or why is this not working?</li>
  <li>Literature to a workaround or links are appreciated?</li>
  </ol>