如何对返回 PartialViewResult 的 MVC Action 进行单元测试?

发布于 2024-10-08 08:51:49 字数 356 浏览 0 评论 0原文

我有一个 Action 如下:

public PartialViewResult MyActionIWantToTest(string someParameter) 
{
    // ... A bunch of logic
    return PartialView("ViewName", viewModel);
}

当我检查结果时,它有一些属性,但它们要么为 null,要么为空。 唯一具有任何内容的属性是 ViewEngineCollection,它不包含任何特定于我的方法的内容。

有人有一些测试 PartialViewResult 的示例代码吗?

I have an Action as follows:

public PartialViewResult MyActionIWantToTest(string someParameter) 
{
    // ... A bunch of logic
    return PartialView("ViewName", viewModel);
}

When I inspect the result, It has a few properties, but they are either null, or empty.
The only property that has anything is the ViewEngineCollection which doesn't contain anything specific to my method.

Does anyone have some example code that tests a PartialViewResult?

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

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

发布评论

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

评论(2

仙气飘飘 2024-10-15 08:51:49

假设您有一个看起来像这样的 Action

public PartialViewResult MyActionIWantToTest(string someParameter)
{
   var viewModel = new MyPartialViewModel { SomeValue = someParameter };
   return PartialView("MyPartialView", viewModel);
}

注意:MyPartialViewModel 是一个简单的类,只有一个属性 - SomeValue

NUnit 示例可能如下所示:

[Test]
public void MyActionIWantToTestReturnsPartialViewResult()
{
    // Arrange
    const string myTestValue = "Some value";
    var ctrl = new StringController();

    // Act
    var result = ctrl.MyActionIWantToTest(myTestValue);

    // Assert
    Assert.AreEqual("MyPartialView", result.ViewName);
    Assert.IsInstanceOf<MyPartialViewModel>(result.ViewData.Model);
    Assert.AreEqual(myTestValue, ((MyPartialViewModel)result.ViewData.Model).SomeValue);
}

Say you have an Action that looks something like this:

public PartialViewResult MyActionIWantToTest(string someParameter)
{
   var viewModel = new MyPartialViewModel { SomeValue = someParameter };
   return PartialView("MyPartialView", viewModel);
}

Note: MyPartialViewModel is a simple class with only one property - SomeValue.

An NUnit example may look like this:

[Test]
public void MyActionIWantToTestReturnsPartialViewResult()
{
    // Arrange
    const string myTestValue = "Some value";
    var ctrl = new StringController();

    // Act
    var result = ctrl.MyActionIWantToTest(myTestValue);

    // Assert
    Assert.AreEqual("MyPartialView", result.ViewName);
    Assert.IsInstanceOf<MyPartialViewModel>(result.ViewData.Model);
    Assert.AreEqual(myTestValue, ((MyPartialViewModel)result.ViewData.Model).SomeValue);
}
深海不蓝 2024-10-15 08:51:49

接受的答案对我不起作用。我执行了以下操作来解决我所看到的测试失败问题。

这就是我的行动:

    [Route("All")]
    public ActionResult All()
    {
        return PartialView("_StatusFilter",MyAPI.Status.GetAllStatuses());
    }

我必须给结果一个类型才能使其工作。我将 PartialViewResult 用于返回部分视图的操作,而不是返回完整视图并使用“查看结果”的其他操作。这是我的测试方法:

 [TestMethod]
 public void All_ShouldReturnPartialViewCalledStatusFilter()
 {
     // Arrange
     var controller = new StatusController();
     // Act
     var result = controller.StatusFilter() as PartialViewResult;
     // Assert
     Assert.AreEqual("_StatusFilter", result.ViewName, "All action on Status Filter  controller did not return a partial view called _StatusFilter.");
  }

The accepted answer did not work for me. I did the following to resolve the test failure I was seeing.

This was my action:

    [Route("All")]
    public ActionResult All()
    {
        return PartialView("_StatusFilter",MyAPI.Status.GetAllStatuses());
    }

I had to give the result a type in order for it to work. I used PartialViewResult for my action returning a Partial View, as opposed to my other actions that return a full view and use View Result. This is my test method:

 [TestMethod]
 public void All_ShouldReturnPartialViewCalledStatusFilter()
 {
     // Arrange
     var controller = new StatusController();
     // Act
     var result = controller.StatusFilter() as PartialViewResult;
     // Assert
     Assert.AreEqual("_StatusFilter", result.ViewName, "All action on Status Filter  controller did not return a partial view called _StatusFilter.");
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文