Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 通过编程执行控制器(使用RoutedData)捕获操作的结果_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 通过编程执行控制器(使用RoutedData)捕获操作的结果

Asp.net mvc 通过编程执行控制器(使用RoutedData)捕获操作的结果,asp.net-mvc,Asp.net Mvc,我从Darin Dimitrov那里找到了以下答案- 唯一的问题是,我喜欢捕获此操作返回的ViewResult(将其呈现为字符串),但IController.Execute返回void 我怀疑我可以在ControllerContext属性的某个地方找到结果,但我找不到类似的东西。有人知道怎么做吗?据我所知,您想做的是实际渲染视图,获取HTML结果并对其进行断言 这实际上是在测试这个几乎不被推荐的视图,并且是针对大多数实践的 但是,您可以想出一些解决方案。其中一个是使用RazorEngine渲染视

我从Darin Dimitrov那里找到了以下答案-

唯一的问题是,我喜欢捕获此操作返回的ViewResult(将其呈现为字符串),但IController.Execute返回
void


我怀疑我可以在ControllerContext属性的某个地方找到结果,但我找不到类似的东西。有人知道怎么做吗?

据我所知,您想做的是实际渲染视图,获取HTML结果并对其进行断言

这实际上是在测试这个几乎不被推荐的视图,并且是针对大多数实践的

但是,您可以想出一些解决方案。其中一个是使用RazorEngine渲染视图(简化且有点凌乱)。由于您无法从测试项目访问.cshtml(视图文件),因此您需要以一种混乱的方式访问其内容

将RazorEngine NuGet软件包安装到您的测试项目中,并尝试以下方法:

    [Fact]
    public void Test()
    {
        var x = new HomeController();  // instantiate controller
        var viewResult = (ViewResult)x.Index(); // run the action and obtain its ViewResult
        var view = string.IsNullOrWhiteSpace(viewResult.ViewName) ? "Index" : viewResult.ViewName;     // get the resulted view name; if it's null or empty it means it is the same name as the action
        var controllerName = "Home"; // the controller name was known from the beginning

        // actually navigate to the folder containing the views; in this case we're presuming the test project is a sibling to the MVC project, otherwise adjust the path to the view accordingly
        var pathToView = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", "");
        pathToView = Path.GetDirectoryName(pathToView);
        pathToView = Path.GetDirectoryName(pathToView);
        pathToView = Path.GetDirectoryName(pathToView);
        pathToView = Path.Combine(pathToView, "WebApplication5\\Views\\" + controllerName + "\\" + view + ".cshtml");

        var html = Razor.Parse(File.ReadAllText(pathToView), viewResult.Model);  // this is the HTML result, assert against it (i.e. search for substrings etc.)
    }

您提供的链接还利用了HttpResponse
    [Fact]
    public void Test()
    {
        var x = new HomeController();  // instantiate controller
        var viewResult = (ViewResult)x.Index(); // run the action and obtain its ViewResult
        var view = string.IsNullOrWhiteSpace(viewResult.ViewName) ? "Index" : viewResult.ViewName;     // get the resulted view name; if it's null or empty it means it is the same name as the action
        var controllerName = "Home"; // the controller name was known from the beginning

        // actually navigate to the folder containing the views; in this case we're presuming the test project is a sibling to the MVC project, otherwise adjust the path to the view accordingly
        var pathToView = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", "");
        pathToView = Path.GetDirectoryName(pathToView);
        pathToView = Path.GetDirectoryName(pathToView);
        pathToView = Path.GetDirectoryName(pathToView);
        pathToView = Path.Combine(pathToView, "WebApplication5\\Views\\" + controllerName + "\\" + view + ".cshtml");

        var html = Razor.Parse(File.ReadAllText(pathToView), viewResult.Model);  // this is the HTML result, assert against it (i.e. search for substrings etc.)
    }