Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用XUnit、Moq和AutoFixture对控制器进行单元测试?_C#_Asp.net Core_Moq_Xunit.net_Autofixture - Fatal编程技术网

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);
}