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