Trying to create a URLHelper for testing purposes throws a NullReferenceException.
Example:
[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);
}
Every time I run this Test, the Exception that is thrown in Url = new UrlHelper(new ActionContext()) is:
Exception.Message:
Message: System.NullReferenceException : Object reference not set to an instance of an object.
Exception.StackTrace:
UrlHelperBase.ctor(ActionContext actionContext) ControllerUnitTest.AuthenticateAsyncTest()
Using:
xUnit 2.4.1, Microsoft.NETCore.App 2.2.0, Microsoft.AspNetCore.Routing.Abstractions 2.2.0
To recreate the Exception:
- Create a empty MVC core 2.2 solution
- Create a xunit test Project
- Install the NuGet Microsoft.AspNetCore.Mvc.Core 2.2.0
- Write in the test: var Url = new UrlHelper(new ActionContext());
- Run test
Should look like this:
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());
}
}
}
My questions:
- Is there a bug, or why is this not working?
- Literature to a workaround or links are appreciated?