C# 如何使用XUnit、Moq和AutoFixture对控制器进行单元测试?
我有一个具有以下签名的控制器:C# 如何使用XUnit、Moq和AutoFixture对控制器进行单元测试?,c#,asp.net-core,moq,xunit.net,autofixture,C#,Asp.net Core,Moq,Xunit.net,Autofixture,我有一个具有以下签名的控制器: public CustomerTypeController( IHttpContextAccessor accessor, IPrincipalProvider provider, IMapper mapper, ILogger<CustomerTypeController> logger, ICustomerTypeService customerTypeService) { } 目前,我的理论如下所示:
public CustomerTypeController(
IHttpContextAccessor accessor,
IPrincipalProvider provider,
IMapper mapper,
ILogger<CustomerTypeController> logger,
ICustomerTypeService customerTypeService)
{ }
目前,我的理论
如下所示:
[Theory, AutoMoqData]
public void GetWhenHasCustomerTypesShouldReturnOneCustomerType(
IFixture fixture,
[Frozen] Mock<ICustomerTypeService> service,
CustomerTypeController sut)
{
//Arrange
var items = fixture.CreateMany<Model.CustomerType>(3).ToList();
//Act
var result = sut.Get(1);
//Assert
Assert.IsType<OkResult>(result);
}
[理论,自动烟雾数据]
public void GetWhenHascustomerTypes应返回一个CustomerType(
IFixture夹具,
[冻结]模拟服务,
客户类型控制器(sut)
{
//安排
var items=fixture.CreateMany(3.ToList();
//表演
var结果=sut.Get(1);
//断言
Assert.IsType(结果);
}
在从控制器获取项目之前,是否需要使用项目设置服务?如果是,服务是如何设置的?请使用[贪婪]属性装饰控制器。 这将确保您的自定义控制器用于创建实例
[Theory, AutoMoqData]
public void GetWhenHasCustomerTypesShouldReturnOneCustomerType(
IFixture fixture,
[Frozen] Mock<ICustomerTypeService> service,
CustomerTypeController sut)
{
//Arrange
var items = fixture.CreateMany<Model.CustomerType>(3).ToList();
//Act
var result = sut.Get(1);
//Assert
Assert.IsType<OkResult>(result);
}