如何对返回 ContentResult 的 ActionResult 进行单元测试?

发布于 2024-08-23 07:41:19 字数 526 浏览 4 评论 0原文

我想对以下 ASP.NET MVC 控制器索引操作进行单元测试。我该如何替换下面断言中的实际参数(用 存根?)。

using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
    public class StatusController : Controller
    {
        public ActionResult Index()
        {
            return Content("Hello World!");
        }
    }
}


[TestMethod]
public void TestMethod1()
{
    // Arrange
    var controller = CreateStatusController();

    // Act
    var result = controller.Index();

    // Assert
    Assert.AreEqual( "Hello World!.", ? );
}

I want to unit test the following ASP.NET MVC controller Index action. What do I replace the actual parameter in the assert below (stubbed with ?).

using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
    public class StatusController : Controller
    {
        public ActionResult Index()
        {
            return Content("Hello World!");
        }
    }
}


[TestMethod]
public void TestMethod1()
{
    // Arrange
    var controller = CreateStatusController();

    // Act
    var result = controller.Index();

    // Assert
    Assert.AreEqual( "Hello World!.", ? );
}

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

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

发布评论

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

评论(3

〃安静 2024-08-30 07:41:19

使用“as”运算符进行可为空的强制转换。然后简单地检查是否有空结果

[TestMethod]
public void TestMethod1()
{
    // Arrange
    var controller = CreateStatusController();

    // Act
    var result = controller.Index() as ContentResult;

    // Assert
    Assert.NotNull(result);
    Assert.AreEqual( "Hello World!.", result.Content);
}

use the "as" operator to make a nullable cast. Then simply check for a null result

[TestMethod]
public void TestMethod1()
{
    // Arrange
    var controller = CreateStatusController();

    // Act
    var result = controller.Index() as ContentResult;

    // Assert
    Assert.NotNull(result);
    Assert.AreEqual( "Hello World!.", result.Content);
}
旧时浪漫 2024-08-30 07:41:19

我喜欢为这类事情创建断言助手。例如,您可能会这样做:

public static class AssertActionResult {
    public static void IsContentResult(ActionResult result, string contentToMatch) {
        var contentResult = result as ContentResult;
        Assert.NotNull(contentResult);
        Assert.AreEqual(contentToMatch, contentResult.Content);        
    }
}

然后您可以这样称呼它:

[TestMethod]
public void TestMethod1()
{
    var controller = CreateStatusController();
    var result = controller.Index();

    AssertActionResult.IsContentResult(result, "Hello World!");    
}

我认为这使得测试更容易阅读和编写。

I like creating assertion helpers for this sort of thing. For instance, you might do something like:

public static class AssertActionResult {
    public static void IsContentResult(ActionResult result, string contentToMatch) {
        var contentResult = result as ContentResult;
        Assert.NotNull(contentResult);
        Assert.AreEqual(contentToMatch, contentResult.Content);        
    }
}

You'd then call this like:

[TestMethod]
public void TestMethod1()
{
    var controller = CreateStatusController();
    var result = controller.Index();

    AssertActionResult.IsContentResult(result, "Hello World!");    
}

I think this makes the tests so much easier to read and write.

疑心病 2024-08-30 07:41:19

您无法测试结果是否不为空,您收到一个 ContentResult 并比较这些值:

[TestMethod]
public void TestMethod1()
{
    // Arrange
    var controller = CreateStatusController();

    // Act
    var result = controller.Index();

    // Assert
    Assert.NotNull(result);
    Assert.IsAssignableFrom(typeof(ContentResult), result);
    Assert.AreEqual( "Hello World!.", result.Content);
}

如果 Nunit 断言格式不正确,我表示歉意,但将其视为伪代码:)

You cant test that the result is not null, that you receive a ContentResult and compare the values:

[TestMethod]
public void TestMethod1()
{
    // Arrange
    var controller = CreateStatusController();

    // Act
    var result = controller.Index();

    // Assert
    Assert.NotNull(result);
    Assert.IsAssignableFrom(typeof(ContentResult), result);
    Assert.AreEqual( "Hello World!.", result.Content);
}

I apoligize if the Nunit asserts aren't welformed, but look at it as pseudo-code :)

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