Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The following constructor parameters did not have matching fixture data

I'm trying to test my controllers using xUnit but getting the following error during execution of Customer Controller:

"The following constructor parameters did not have matching fixture data: CustomerController customerController"

Test Class

public class UnitTest1
{
    CustomerController _customerController;

    public UnitTest1(CustomerController customerController)
    {
        _customerController = customerController;
    }

    [Fact]
    public void PostTestSuccessful()
    {
        Guid guid = Guid.NewGuid();

        CustomerViewModel model = new CustomerViewModel()
        {
            Id = guid,
            Name = "testName",
            Email = "test email",
            PhoneNumber = "test phone",
            Address = "test address",
            City = "test city",
            Gender = "Male"
        };

        var actionResult = _customerController.Post(model);

        Assert.NotNull(actionResult);
        Assert.IsType<Task<IActionResult>>(actionResult);
        Assert.True(actionResult.IsCompletedSuccessfully);
    }

CustomerController Class

[Route("customers")]
public class CustomerController : ControllerBase
{
    private readonly ILogger _logger;
    private readonly ICustomerService _customerService;

    public CustomerController(ILogger<CustomerController> logger,
        ICustomerService customerService)
    {
        _logger = logger;
        _customerService = customerService;
    }

    [HttpPost]
    public async Task<IActionResult> Post([FromBody] CustomerViewModel viewModel)
    {
        var customerToBeSaved = viewModel.Adapt<CustomerServiceModel>();

        var customer = await _customerService.SaveAsync(customerToBeSaved);

        var result = customer.Adapt<CustomerViewModel>();

        return Ok(result);
    }
like image 604
MePengusta Avatar asked Jul 03 '18 13:07

MePengusta


3 Answers

What you are missing is the IClassFixture interface for the test class. This will fix the problem...

public class UnitTest1 : IClassFixture<CustomerController>
like image 146
AQuirky Avatar answered Oct 16 '22 22:10

AQuirky


Just new up CustomerController in the constructor, if you don't want to use any mocking framework.

like image 10
user9410863 Avatar answered Oct 17 '22 00:10

user9410863


This article shows how to get xunit working with .Net Core ASP.Net really well. It actually replaces the startup so that your controllers run in the same process, and you can test them as if they were local.

https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests

It allows your standard .Net Dependency Injection to work as it normally does. Moreover it has the amazing benefit of not running as a server, and it fakes the whole startup process so that it runs in one single process and you can debug all the way through. This is also the way you should do it because Microsoft says so.

There's more help to be gleaned from the forum at the bottom of the article.

like image 10
Bluebaron Avatar answered Oct 16 '22 22:10

Bluebaron