MVC moq 在 RedirecToAction() 之前对对象进行单元测试

发布于 2024-08-06 06:09:03 字数 1595 浏览 3 评论 0原文

我想在重定向到另一个操作之前测试“item”对象内的数据。

     public ActionResult WebPageEdit(WebPage item, FormCollection form)
    {
        if (ModelState.IsValid)
        {

            item.Description = Utils.CrossSiteScriptingAttackCheck(item.Description);
            item.Content = Utils.CrossSiteScriptingAttackCheck(item.Content);
            item.Title = item.Title.Trim();
            item.DateUpdated = DateTime.Now;

           // Other logic stuff here

            webPagesRepository.Save(item);


            return RedirectToAction("WebPageList");
        }

这是我的测试方法:

[Test]
    public void Admin_WebPageEdit_Save()
    {

        var controller = new AdminController();

        controller.webPagesRepository = DataMock.WebPageDataInit();
        controller.categoriesRepository = DataMock.WebPageCategoryDataInit();

        FormCollection form = DataMock.CreateWebPageFormCollection();


        RedirectToRouteResult actionResult = (RedirectToRouteResult)controller.WebPageEdit(webPagesRepository.Get(1), form);
        Assert.IsNotNull(actionResult);

        Assert.AreEqual("WebPageList", actionResult.RouteValues["action"]);


        var item = ((ViewResult)controller.WebPageEdit(webPagesRepository.Get(1), form)).ViewData.Model as WebPage;

        Assert.NotNull(item);



        Assert.AreEqual(2, item.CategoryID);




    }

它在这一行失败了:

var item = ((ViewResult)controller.WebPageEdit(webPagesRepository.Get(1), form)).ViewData.Model as WebPage;

我正在考虑在重定向到其他操作之前是否有任何方法可以测试“item”对象?

I want to test the data inside the "item" object before it redirect to another action.

     public ActionResult WebPageEdit(WebPage item, FormCollection form)
    {
        if (ModelState.IsValid)
        {

            item.Description = Utils.CrossSiteScriptingAttackCheck(item.Description);
            item.Content = Utils.CrossSiteScriptingAttackCheck(item.Content);
            item.Title = item.Title.Trim();
            item.DateUpdated = DateTime.Now;

           // Other logic stuff here

            webPagesRepository.Save(item);


            return RedirectToAction("WebPageList");
        }

Here is my Test method:

[Test]
    public void Admin_WebPageEdit_Save()
    {

        var controller = new AdminController();

        controller.webPagesRepository = DataMock.WebPageDataInit();
        controller.categoriesRepository = DataMock.WebPageCategoryDataInit();

        FormCollection form = DataMock.CreateWebPageFormCollection();


        RedirectToRouteResult actionResult = (RedirectToRouteResult)controller.WebPageEdit(webPagesRepository.Get(1), form);
        Assert.IsNotNull(actionResult);

        Assert.AreEqual("WebPageList", actionResult.RouteValues["action"]);


        var item = ((ViewResult)controller.WebPageEdit(webPagesRepository.Get(1), form)).ViewData.Model as WebPage;

        Assert.NotNull(item);



        Assert.AreEqual(2, item.CategoryID);




    }

It failed at this line:

var item = ((ViewResult)controller.WebPageEdit(webPagesRepository.Get(1), form)).ViewData.Model as WebPage;

I am thinking about is there any ways to test the "item" object before it redirect to other actions?

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

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

发布评论

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

评论(1

眼波传意 2024-08-13 06:09:03

您可能知道,它失败的原因是您的控制器操作永远不会返回 ViewResult,而只会返回 RedirectToRouteResult (假设您没有省略某些代码路径)。您可以在重定向到的操作中返回 ViewResult,但您不会在此处测试该操作。

在重定向之前确定和测试项目对象状态的最佳方法是它与 webPagesRepository 对象的 Save() 方法的交互。据推测,从测试方法的名称来看,您想测试该方法是否确实被调用。因此,您应该对 Save() 方法抱有期望。在该期望内,您可以检查并断言传递给它的项目对象的状态。这假设您的 webPagesRepository 是一个模拟依赖项。

我是 Rhino Mocks 用户,所以只知道如何使用该 API 设置期望。如果您觉得它有用,请告诉我,我会发布它。

The reason it failed, as you probably know, is that your controller action never returns a ViewResult, only a RedirectToRouteResult (assuming you haven't ellided some code path). You may return a ViewResult in the action you're redirecting to, but you are not testing that action here.

The best way to ascertain and test the state of your item object prior to redirection is its interaction with the Save() method of your webPagesRepository object. Presumably, from the name of test method, you want to test that this method is actually being called anyway. Therefore you should have an expectation on the Save() method. Within that expectation you can then examine and assert the state of the item object that is passed to it. This assumes that your webPagesRepository is a mocked dependency.

I'm a Rhino Mocks user and so only know how to set up the expectation using that API. Let me know if you'd find it useful nevertheless and I'll post it.

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