49

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);
    }
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
MePengusta
  • 763
  • 1
  • 8
  • 18
  • 6
    `public UnitTest1(CustomerController customerController)` Your unit test needs to new up the controller (i.e. you need to remove the constructor parameter). There is no magical dependency injection that will inject it for you. – mjwills Jul 03 '18 at 13:36
  • 2
    You should look at mocking libraries to mock your dependencies for `CustomerController`. I personally like `moq`, but there are loads to choose from. `Rhino`, `SimpleMock` off the top of my head. – Jamiec Jul 03 '18 at 15:16

4 Answers4

42

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

public class UnitTest1 : IClassFixture<CustomerController>
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
AQuirky
  • 4,691
  • 2
  • 32
  • 51
12

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

user9410863
  • 755
  • 6
  • 4
8

For the testing framework, you need the mocking library to inject a mock object through DI in your testing classes. You can use Nmock, Moq or any other mocking library to setup the constructor injection.

https://www.c-sharpcorner.com/uploadfile/john_charles/mocking-in-net-with-moq/

http://nmock.sourceforge.net/quickstart.html

Gaurav Jalan
  • 467
  • 2
  • 14
-1

Your Test Class Need to Inject ILogger and ICustomerService in Constructor

I Suggest you to Use Mock and Inject Controller Constructor Inputs in Test Class with Mocking

But Don't forget to Set Up the Mocked Objects or Interfaces

Learn More About Mock Here.....enter link description here

Javid_leo
  • 1
  • 1