Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.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/5/fortran/2.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#controller中对HTTP响应/请求进行单元测试?_C#_Unit Testing_.net Core - Fatal编程技术网

如何在c#controller中对HTTP响应/请求进行单元测试?

如何在c#controller中对HTTP响应/请求进行单元测试?,c#,unit-testing,.net-core,C#,Unit Testing,.net Core,我试图对一个c#控制器方法进行单元测试。我查了很多问题,但没有任何东西能告诉我如何对API控制器进行单元测试。下面是该方法的正确代码。如何对括号进行单元测试 我已经知道了如何测试“反向支付”方法,但我正在使用moq和xunit来测试实际的c#方法。这是我当前的单元测试: 公共类付款控件测试:BackOfficeIntegrationTestBase { 私有只读CurrentDateProvider\u CurrentDateProvider; [BackOfficeUsersAuthorize

我试图对一个c#控制器方法进行单元测试。我查了很多问题,但没有任何东西能告诉我如何对API控制器进行单元测试。下面是该方法的正确代码。如何对括号进行单元测试

我已经知道了如何测试“反向支付”方法,但我正在使用moq和xunit来测试实际的c#方法。这是我当前的单元测试:

公共类付款控件测试:BackOfficeIntegrationTestBase
{
私有只读CurrentDateProvider\u CurrentDateProvider;
[BackOfficeUsersAuthorize(BackOfficeUserRole.Admin、BackOfficeUserRole.CSR、BackOfficeUserRole.DealerSupportRep、,
BackOfficeUserRole.CSRManager)]
[事实]
公共异步任务AdminReversalPermissions()
{
IFixture fixture=new fixture().Customize(new AutoMoqCustomization());
var mediator=fixture.Freeze();
var currentDateProvider=fixture.Freeze();
var mockRepo=new Mock();
var控制器=新的付款控制器(mockRepo.Object);
//设置依赖项
//DataContext DataContext=设置数据库(夹具);
反向支付设置(夹具、中介);
var结果=等待控制器反向支付(LeaseInfo.ApplicationId,100);
var viewResult=结果为NOCONNTRESULT;
Assert.NotNull(viewResult);
}
私有void ReversePaymentSetup(iTexture装置、模拟中介)
{
PaymentData=新的PaymentData();
//嘲笑调解人
var paymentPlan=new Data.Model.paymentPlan()
{
LeaseId=1,
最小值=最小值年,
延期天数=5天,
FirstPaymentDate=DateTime.ParseExact(“2019年1月1日”,“d”,CultureInfo.InvariantCulture),
StoreState=“VA”
};
var responseData=新建BuildPaymentPlanResponse();
responseData.Data=新建BuildPaymentPlanResponse.InitializePaymentPlanResponseData()
{
PaymentPlan=PaymentPlan
};
PaymentStatusChangeManualRequest请求=fixture.Build()
.With(x=>x.PaymentPlanId,LeaseInfo.ApplicationId)
.With(x=>x.PaymentId,100)
.With(x=>x.Status,PaymentStatus.Reversed)
.Create();
Assert.Equal(PaymentStatus.Reversed,request.Status);
Assert.Equal(100,request.PaymentId);
Assert.NotNull(LeaseInfo.ApplicationId);
}
}
}
[ProducesResponseType(typeof(ErrorResponse),(int)HttpStatusCode.NotFound]
[HttpPut(“{paymentId}/ReversePayment”)]
[BackOfficeUsersAuthorize(BackOfficeUserRole.Admin、BackOfficeUserRole.CSR、BackOfficeUserRole.DealerSupportRep、,
BackOfficeUserRole.CSRManager)]
公共异步任务反向支付(int paymentPlanId,int paymentId)
{
等待调解人发送(新付款状态更改手动请求)
{
PaymentPlanId=PaymentPlanId,
PaymentId=PaymentId,
状态=付款状态。已反转
});
返回NoContent();
} 

不确定“括号的单元测试”是什么意思?不管怎样,您对控制器进行了单元测试,您认为您在这里缺少了什么?[ProductsResponseType]和HttpPut的部分,您如何对它们进行单元测试?首先,您认为为什么需要测试属性?其次,
ProducesResponseType
属性声明您的方法返回一个
NotFound
状态,但代码根本不这样做。
[ProducesResponseType(typeof(ErrorResponse), (int)HttpStatusCode.NotFound)]
        [HttpPut("{paymentId}/ReversePayment")]
        [BackOfficeRolesAuthorize(BackOfficeUserRole.Admin, BackOfficeUserRole.CSR, BackOfficeUserRole.DealerSupportRep,
            BackOfficeUserRole.CSRManager)]
        public async Task<IActionResult> ReversePayment(int paymentPlanId, int paymentId)
        {
            await _mediator.Send(new PaymentStatusChangeManualRequest
            {
                PaymentPlanId = paymentPlanId,
                PaymentId = paymentId,
                Status = PaymentStatus.Reversed
            });

            return NoContent();
        }