单元测试 MVC 控制器

发布于 2024-12-19 09:21:17 字数 1748 浏览 5 评论 0原文

我正在尝试结合 IoC 和 DI 来学习 MVC。在我的项目中我还使用 Castle、Automapper、NHibernate、NHibernateIntegration。

感谢 Kozmic 的优秀示例“ToBeSeen”应用程序,我想我现在已经基本设置了解决方案。但现在我想在控制器方法级别对基本保存操作进行单元测试。我只是不知道该怎么做。我希望我的问题非常简单,但我是这方面的新手,感谢任何帮助。

我有一个像这样的档案控制器:

    [Authorize]
    [Transactional]
    public class DossierController : BaseController
    {
        private readonly IRepository repository;
        private readonly IMappingEngine mappingEngine;
        private readonly ILogger logger;

        public DossierController(IRepository repository, IMappingEngine mappingEngine, ILogger logger)
        {
            this.repository = repository;
            this.mappingEngine = mappingEngine;
            this.logger = logger;
        }

如您所见,它需要一个存储库、映射引擎和记录器。据我了解,所有这些都是在运行时配置和连接的。

接下来,我有一个用于保存达析报告的控制器方法:

        [HttpPost]
        [ActionName("Dossier")]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        [AcceptParameter(Name = "button", Value = "save")]
        public ActionResult Dossier_Save(string button, DossierModel dossierModel, string returnUrl)
        {
            if (!Request.IsAuthenticated)
                return RedirectToAction("Index", "Home");

            if (!ModelState.IsValid) return View(dossierModel);

            Dossier dossier = mappingEngine.Map<DossierModel, Dossier>(dossierModel);

            repository.Save(dossier);

            return View();
        }

这里我只想将达析报告模型自动映射到达析报告中,并使用存储库保存它。

现在我想使用 Nunit 对其进行单元测试。我不想模拟任何东西,我想要从控制器级别进行真正的持久性测试。为此,我必须创建一个新的 dossiercontroller 并传递正确的参数。现在我迷路了。如何创建和配置参数,以便它们像在 Web 应用程序中一样工作。例如:如何在单元测试中创建正确配置的自动映射器?如何创建正确配置的存储库?我应该使用容器吗?

非常感谢任何帮助,特别是代码示例!

提前致谢。

Im trying to learn MVC combined with IoC and DI. In my project I also use Castle, Automapper, NHibernate, NHibernateIntegration.

Thanks to the excellent example "ToBeSeen" app by Kozmic I think I have the solution pretty much set up now. But now I want to unittest basic save operation at the controller method level. I just dont get how to go about this. I expect my problem is pretty simple but Im a novice at this and any help is appreciated.

I have a dossiercontroller like this:

    [Authorize]
    [Transactional]
    public class DossierController : BaseController
    {
        private readonly IRepository repository;
        private readonly IMappingEngine mappingEngine;
        private readonly ILogger logger;

        public DossierController(IRepository repository, IMappingEngine mappingEngine, ILogger logger)
        {
            this.repository = repository;
            this.mappingEngine = mappingEngine;
            this.logger = logger;
        }

As you can see it needs a repostitory, mappingengine and logger. As I understand all this is configured and wired up at runtime.

Next I have a controller method that is used to save a dossier:

        [HttpPost]
        [ActionName("Dossier")]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        [AcceptParameter(Name = "button", Value = "save")]
        public ActionResult Dossier_Save(string button, DossierModel dossierModel, string returnUrl)
        {
            if (!Request.IsAuthenticated)
                return RedirectToAction("Index", "Home");

            if (!ModelState.IsValid) return View(dossierModel);

            Dossier dossier = mappingEngine.Map<DossierModel, Dossier>(dossierModel);

            repository.Save(dossier);

            return View();
        }

Here I just want to automap the dossiermodel into a dossier and save it using the repository.

Now I want to unit test this using Nunit. I dont want to mock anything, I want a real persistence test from the controller level. For this I have to create a new dossiercontroller and pass in the correct arguments. Now Im lost. How do I create and configure the arguments so they work exactly like in the web application. For instance: How do I create a correctly configured Automapper in a Unittest? How do I create a correctly configured repository? Should I use a container?

Any help, especcialy a code example is highly appreciated!

Thanks in advance.

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

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

发布评论

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

评论(1

七颜 2024-12-26 09:21:17

如果您不想模拟任何内容,则需要实例化控制器构造函数所需的服务的具体版本。

例如,

IRepository repository = GetRepository();
IMappingEngine mappingEngine = GetMappingEngine();
ILogger logger = GetLogger();
DossierController controller = new DossierController(repository, mappingEngine, logger);
ActionResult rsult = controller.Save(...etc...);

至于如何配置和实例化它们,您基本上需要以与 MVC 应用程序完全相同的方式执行此操作。

如果您不确定 MVC 应用程序中的何处配置这些内容,我建议首先查看 IoC 配置并从那里开始工作。

If you don't want to mock anything, you will need to instantiate concrete versions of the services required by the constructor of your controller.

e.g.

IRepository repository = GetRepository();
IMappingEngine mappingEngine = GetMappingEngine();
ILogger logger = GetLogger();
DossierController controller = new DossierController(repository, mappingEngine, logger);
ActionResult rsult = controller.Save(...etc...);

As to how you are to configure and instantiate these, you will essentially need to do this in exactly the same way that your MVC app is doing this.

If you are unsure where in your MVC app these are being configured, I'd suggest that the first place to look is in the IoC configuration and work backword from there.

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