C# 以下构造函数参数没有匹配的夹具数据

C# 以下构造函数参数没有匹配的夹具数据,c#,.net,unit-testing,.net-core,xunit,C#,.net,Unit Testing,.net Core,Xunit,我正在尝试使用xUnit测试我的控制器,但在执行Customer Controller时出现以下错误: “以下构造函数参数没有匹配的fixture 数据:CustomerController“CustomerController” 测试类 public class UnitTest1 { CustomerController _customerController; public UnitTest1(CustomerController customerController)

我正在尝试使用
xUnit
测试我的控制器,但在执行Customer Controller时出现以下错误:

“以下构造函数参数没有匹配的fixture 数据:CustomerController“CustomerController”

测试类

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);
    }
[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);
    }
公共类UnitTest1
{
客户控制器\u客户控制器;
公共单元测试1(CustomerController CustomerController)
{
_customerController=customerController;
}
[事实]
public void PostTestSuccessful()
{
Guid=Guid.NewGuid();
CustomerViewModel模型=新CustomerViewModel()
{
Id=guid,
Name=“testName”,
Email=“测试电子邮件”,
PhoneNumber=“测试电话”,
Address=“测试地址”,
City=“测试城市”,
性别=“男性”
};
var actionResult=\u customerController.Post(模型);
Assert.NotNull(actionResult);
Assert.IsType(actionResult);
Assert.True(actionResult.IsCompletedSuccessfully);
}
客户控制器类

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);
    }
[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);
    }
[路线(“客户”)]
公共类CustomerController:ControllerBase
{
专用只读ILogger\u记录器;
专用只读ICCustomerService(用户服务);
公共客户控制器(ILogger logger,
ICustomerService(客户服务)
{
_记录器=记录器;
_customerService=customerService;
}
[HttpPost]
公共异步任务发布([FromBody]CustomServiceWModel viewModel)
{
var customerToBeSaved=viewModel.Adapt();
var customer=wait\u customerService.SaveAsync(customerToBeSaved);
var result=customer.Adapt();
返回Ok(结果);
}

对于测试框架,您需要模拟库通过DI在测试类中注入模拟对象。您可以使用Nmock、Moq或任何其他模拟库来设置构造函数注入


如果您不想使用任何模拟框架,只需在构造函数中新建CustomerController。

本文介绍如何让xunit与.Net核心ASP.Net很好地协同工作。它实际上取代了启动,以便您的控制器在同一进程中运行,并且您可以像在本地一样测试它们

它允许您的标准.Net依赖项注入正常工作。此外,它还具有不作为服务器运行的惊人优势,它伪造了整个启动过程,使其在单个进程中运行,您可以全程调试。这也是您应该采用的方式,因为Microsoft这么说


文章底部的论坛提供了更多帮助。

您缺少的是测试类的IClassFixture接口。这将解决问题

 public class UnitTest1 : IClassFixture<CustomerController>
公共类UnitTest1:IClassFixture

公共单元测试1(CustomerController CustomerController)
您的单元测试需要更新控制器(即,您需要删除构造函数参数)。没有神奇的依赖项注入可以为您注入。您应该查看模拟库来模拟
CustomerController
的依赖项。我个人喜欢
moq
,但有很多负载可供选择。
Rhino
SimpleMock
。我也在跟踪相同的链接。在哪里我可以为工厂配置DI吗?我不知道你在说什么环境下。DI应该在.Net Core中自动启用。可能必须看看.Net Core中的DI。我说的是单元测试。但它现在正在工作。谢谢你的解释。非常有帮助。把它们复制到你的答案中,我保证我会投赞成票。只要提供链接有时对答案来说太短,特别是如果链接的文章很长的话。现在看起来不是更好了吗?我编辑并更新了你的答案,请在你未来的答案中更具描述性。不要假设每个人都知道你所知道的。“新建CustomerController”?@DMur他们的意思是在类构造函数中实例化控制器,例如
\u myPrivateProperty=new CustomerController();