【问题标题】:MVCContrib - What's the best way to test XMLResult?MVCContrib - 测试 XMLResult 的最佳方法是什么?
【发布时间】:2010-12-10 04:25:47
【问题描述】:

我刚刚开始使用 MVCContrib。我有一个返回 XMLResult 的控制器操作。我想围绕 XMLResult 编写单元测试。我从未使用过 MVCContrib 的 TestHelper。如何从 XMLResult 获取生成的 XML 进行测试?我需要模拟出 HttpContext obj 吗?

谢谢

【问题讨论】:

    标签: asp.net-mvc mvccontrib


    【解决方案1】:

    假设以下动作:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                Name = "hello"
            };
            return new XmlResult(model);
        }
    }
    

    你可以:

    // arrange
    var sut = new HomeController();
    
    // act
    var actual = sut.Index();
    
    // assert
    actual
        .AssertResultIs<XmlResult>()
        .ObjectToSerialize
        .ShouldBe<MyViewModel>("")
        .Name
        .ShouldEqual("hello", "");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2020-12-25
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      相关资源
      最近更新 更多