本文介绍了以下构造函数参数没有匹配的灯具数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 xUnit
测试我的控制器,但是在执行Customer Controller的过程中出现以下错误:
I'm trying to test my controllers using xUnit
but getting the following error during execution of Customer Controller:
以下构造函数参数没有匹配的夹具
数据: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);
}
CustomerController类
[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);
}
推荐答案
对于测试框架,您需要模拟库通过测试类中的DI注入模拟对象。您可以使用Nmock,Moq或任何其他模拟库来设置构造函数注入。
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.
这篇关于以下构造函数参数没有匹配的灯具数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!