0
我正在使用Castle Windsor作为DI并使用reposity来访问和实现数据层。 由于我已经在我的仓库中实现了所有的数据访问层,现在可以在我的API控制器中调用这些方法了。所以我有'getAllReportsByClient方法'和'CreateReport'POST方法。因此,为了测试某些方法是否在没有实际执行视图和AJAX调用的情况下工作,我怎样才能使用我的“创建报告”方法插入样本数据?通过传递模型测试HTTPPOST请求的最佳方式是什么?
从回购的方法如下:
public void CreateReport(TReportHeaderModel model)
{
using (var connection = new TReportEntitiesConnection())
{
connection.THeader.Add(new THeader()
{
ClientID=model.ClientID,
ID=model.ID,
THeaderTitle=model.THeaderTitle,
RowNumber=model.RowNumber
});
foreach (var d in model.TReports)
{
connection.TReport.Add(new TReport()
{
ID=d.ID,
TReportName=d.TReportName,
URL=d.URL,
RowNumber=d.RowNumber,
});
}
connection.SaveChanges();
}
throw new NotImplementedException();
}
下面是HTTPPOST CREATEREPORT调用控制器:
[HttpPost]
public ActionResultModel CreateReport([FromBody] TReportHeaderModel model)
{
try
{
_tReportingService.CreateReport(model);
return new ActionResultModel() //return void, must not be followed by object expression
{
Success = true,
Message = "Report Successfully Created."
};
}
catch (Exception ex)
{
return new ActionResultModel()
{
Success = false,
Message = "Report not created.",
Obj=ex.Message
};
}
}