如何对返回 ContentResult 的 ActionResult 进行单元测试?
我想对以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用“as”运算符进行可为空的强制转换。然后简单地检查是否有空结果
use the "as" operator to make a nullable cast. Then simply check for a null result
我喜欢为这类事情创建断言助手。例如,您可能会这样做:
然后您可以这样称呼它:
我认为这使得测试更容易阅读和编写。
I like creating assertion helpers for this sort of thing. For instance, you might do something like:
You'd then call this like:
I think this makes the tests so much easier to read and write.
您无法测试结果是否不为空,您收到一个
ContentResult
并比较这些值:如果 Nunit 断言格式不正确,我表示歉意,但将其视为伪代码:)
You cant test that the result is not null, that you receive a
ContentResult
and compare the values:I apoligize if the Nunit asserts aren't welformed, but look at it as pseudo-code :)