The following constructor parameters did not have matching fixture data

MePengusta picture MePengusta · Jul 3, 2018 · Viewed 33.6k times · Source

I'm trying to test my controllers using xUnitbut 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);
    }

Answer

user9410863 picture user9410863 · Nov 7, 2018

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