2011-05-05 38 views
0
[CompanyAuthorizationFilter] 
    [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)] 
    public class TermSheetController : System.Web.Mvc.Controller 
    { 
     public ActionResult Notes(Guid? id, string notesText) 
     { 
      if (isModelValid(id)) 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", notesText); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult SummaryInformation(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       return View(getSpecificViewPath(model) + "SummaryInformation.aspx", model); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult ProductLegs(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       return View(getSpecificViewPath(model) + "ProductLegs.aspx", model); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult AmortizationOptions(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       return View(getSpecificViewPath(model) + "AmortizationOptions.aspx", model); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult Values(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       return View(getSpecificViewPath(model) + "Values.aspx", model); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult Rates(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       return View(getSpecificViewPath(model) + "Rates.aspx", model); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult RatesSpecific(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       return View(getSpecificViewPath(model) + "RatesSpecific.aspx", model); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult AmortizationSchedule(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       return View(getSpecificViewPath(model) + "AmortizationSchedule.aspx", model); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult SponsorInfo(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       return View(getBaseViewPath() + "SponsorInfo.aspx", model); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult BorrowerInfo(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       return View(getBaseViewPath() + "BorrowerInfo.aspx", model); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult SponsorContacts(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       return View(getBaseViewPath() + "SponsorContacts.aspx", model); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult CashFlows(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       return View(getSpecificViewPath(model) + "CashFlows.aspx", model); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult PrePayment(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       if (string.IsNullOrEmpty(model.PrepaymentExport)) 
       { 
        return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Prepayment has not been calculated yet."); 
       } 
       else 
       { 
        return Json(model.PrepaymentExport); 
       } 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult FutureExposure(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       if (string.IsNullOrEmpty(model.ExposureExport)) 
       { 
        return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Potential Future Exposure has not been calculated yet."); 
       } 
       else 
       { 
        return Json(model.ExposureExport); 
       } 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult FutureExposureSpecific(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       if (string.IsNullOrEmpty(model.ExposureExport)) 
       { 
        return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Potential Future Exposure has not been calculated yet."); 
       } 
       else 
       { 
        return Json(model.ExposureExport); 
       } 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult History(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       return View(getBaseViewPath() + "History.aspx", model); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     public ActionResult ValuationHistoryGrid(Guid? id) 
     { 
      if (isModelValid(id)) 
      { 
       ModelBase model = getModel(id.Value); 
       return View(getSpecificViewPath(model) + "ValuationHistoryGrid.aspx", model); 
      } 
      else 
      { 
       return View(@"~\Views\Indications\ShowAStringPartial.aspx", "", "Structure is not in a valid state. Could not render Term Sheet View."); 
      } 
     } 

     private bool isModelValid(Guid? id) 
     { 
      try 
      { 
       if (id.HasValue) 
       { 
        IndicationBase indication = CachedTransactionManager<IndicationBase>.GetCachedTransactions(id.Value); 
        if (indication.Model == null) 
        { 
         return false; 
        } 
        else 
        { 
         return true; 
        } 
       } 
       else 
       { 
        return false; 
       } 
      } 
      catch (Exception e) 
      { 
       return false; 
      } 
     } 

     private ModelBase getModel(Guid id) 
     { 
      IndicationBase indication = CachedTransactionManager<IndicationBase>.GetCachedTransactions(id); 
      return indication.Model; 
     } 

     private string getSpecificViewPath(ModelBase model) 
     { 
      var viewPath = ""; 
      if (model.GetType() == typeof(Chatham.Web.Models.Indications.SwapModel)) 
      { 
       viewPath = @"~\Views\Indications\TermSheetViews\Swap\"; 
      } 
      if (model.GetType() == typeof(Chatham.Web.Models.Indications.OptionModel)) 
      { 
       viewPath = @"~\Views\Indications\TermSheetViews\Option\"; 
      } 
      if (model.GetType() == typeof(Chatham.Web.Models.Indications.CollarModel)) 
      { 
       viewPath = @"~\Views\Indications\TermSheetViews\Collar\"; 
      } 
      if (model.GetType() == typeof(Chatham.Web.Models.Indications.CompoundSwapModel)) 
      { 
       viewPath = @"~\Views\Indications\TermSheetViews\CompoundSwap\"; 
      } 

      return viewPath; 
     } 

     private string getBaseViewPath() 
     { 
      return @"~\Views\Indications\TermSheetViews\"; 
     } 
    } 

所以,只需查看代碼,您就可以知道它的功能。每個控制器操作引用了不同的期望結果,並且我正在傳入(大部分)Guid? id,我們正在使用該id調用private輔助方法,獲取model,並返回基於特定的view在那model測試我的MVC2控制器

我真的不知道從哪裏開始單元測試這門課。我能在這裏得到一些幫助嗎?謝謝!

回答

0

好吧,首先,您應該測試以確保具有正確視圖位置集的ViewResult中的特定Guid傳遞結果。 AKA

ActionResult action = _myControllerToTest.Notes(null, null); 
Assert.IsNotNull(action); 

ViewResult viewResult = action as ViewResult; 
Assert.IsNotNull(viewResult); 

// Check viewResult for correct view path and model data 

開始出現,並確保注入depdencies(特別是對於isModelValid()方法,因爲它召喚出CachedTransactionManager)

+0

我能用嘲笑CachedTransactionManager到一個特定的模式迴歸我每次而不是使用依賴注入? – slandau 2011-05-05 15:00:43

+0

正確。藉助像Moq這樣的幫助程序庫,您可以注入模擬對象以每次都返回特定的結果。 – Tejs 2011-05-05 15:15:55