2017-10-06 57 views
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 

      }; 


     } 


    } 

回答

0

你可以使用邮差(https://www.getpostman.com/)或小提琴手(www.telerik.com/小提琴手)来模拟请求。更好的是,你可以使用测试框架编写测试。

相关问题