I wrote a unit test using xUnit to test a method in my ASP.net core Web API controller.
 public class RegistrationsControllerTest : ControllerTestBase<RegistrationsController>
{
    RegistrationsController registrationsController;
    public RegistrationsControllerTest()
    {
        registrationsController = new RegistrationsController(log, registrationRepository);
    }
    [Fact]
    public void Add_InvalidObjectPassed_ReturnsBadRequest()
    {
        // Arrange
        var postBackMissingItem = new RegistrationFilter()
        {
            Id = Guid.NewGuid().ToString(),
            PostbackURI = "https://test.example.com",
            FailureURI = "https://testfail.example.com"
        };
        postBackMissingItem.PostbackURI = null;          
        // Act
        var badResponse = registrationsController.AddRegistration(postBackMissingItem);
        // Assert
        Assert.IsType<BadRequestObjectResult>(badResponse);
    }
}
I am trying to validate the input using TryValidateModel in my controller method.
public class RegistrationsController : ControllerBase
{
    ILogger<RegistrationsController> log;
    IRegistrationRepository registrationRepository;
    public RegistrationsController(ILogger<RegistrationsController> log, IRegistrationRepository registrationRepository)
    {
        this.log = log;
        this.registrationRepository = registrationRepository;
        RegistrationsController regController = new RegistrationsController(this.log, this.registrationRepository);
        regController.ControllerContext = new ControllerContext();
    }
    [HttpPost(Name = nameof(AddRegistration))]
    public async Task<IActionResult> AddRegistration([FromBody] RegistrationFilter registrationRequest)
    {
        // Ensure Id=null for inserts
        registrationRequest.Id = null;
        // Validate request content
        if (!TryValidateModel(registrationRequest))
        {
            var errorText = "BadRequest. ModelState invalid";
            log.ErrorEx(tr => tr.SetObject(new { Registration = registrationRequest }, errorText));
            return BadRequest(ModelState); 
        }
        // Some code
    }
}
When I debug the unit test, it throws error at the line which has TryValidateModel.
The error is Object reference not set to an instance of an object. I have gone through similar issues on Stack Overflow where they mention about mocking TryValidateModel but I don't want to mock it.
In this issue here How do I Unit Test Actions without Mocking that use UpdateModel?, one guy mentions about adding default controller context to the controller.
I tried that approach but doing so exits debug mode when trying to debug my test and an error
The program '[25720] dotnet.exe' has exited with code -1073741819 (0xc0000005) Access violation
Is shown in the output window.
In the controller method constructor I added the ControllerContext lines but debug mode exits with the error I mentioned.
Did I add that incorrectly or at a wrong place? What modifications should be done to pass my unit test?
User contributions licensed under CC BY-SA 3.0