测试我的 MVC2 控制器

发布于 2024-11-05 07:04:04 字数 11177 浏览 0 评论 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,并返回特定的<基于该模型的code>view。

我真的不知道从哪里开始对这个类进行单元测试。我可以在这里得到一些帮助吗?谢谢!

[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\";
        }
    }

So just looking through the code you can probably tell what it does. Each controller action refers to a different desired result, and I'm passing in (for the most part), a Guid? id, and we are using that id to call into the private helper methods, get the model, and return a specific view based on that model.

I really don't even know where to begin to unit test this class. Can I get some help here? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-11-12 07:04:04

好吧,首先,您应该进行测试以确保传入的特定 Guid 会产生具有正确视图位置集的 ViewResult。 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

从那里开始,并确保注入依赖项(特别是 isModelValid() 方法,因为它调用 CachedTransactionManager)

Well, to start with, you should test to make sure that a specific Guid passed in results in a ViewResult with the right View location set. 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

Start there, and make sure to inject the depdencies (particularly for that isModelValid() method, since it calls out to CachedTransactionManager)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文