gpt4 book ai didi

asp.net-core - Asp.Net Core 2.0 Xunit 测试

转载 作者:行者123 更新时间:2023-12-04 17:49:51 26 4
gpt4 key购买 nike

我是新的 .asp.net 核心。我正在测试一个将 View 呈现为字符串然后利用 evo pdf 呈现 View 的 Controller 。

一切正常,我也能够成功地使用 postman 进行测试。但是,当我使用 vs 2017 测试资源管理器调试我的测试 (Xunit) 时,我的测试应用程序出错。 Searched Locations within the razor engine该错误发生在我的 RenderViewToString 方法中,因为我的 Razor View 引擎无法找到要呈现的 View 。为查找 View 而搜索的路径符合预期。任何指导表示赞赏。

    //Unit Test Code
[Fact]
public async void GetPdf()
{
var response = await _client.PostAsJsonAsync<Common.DTO.Invoice>("/api/values/1", GetDummyData());

using (var file = System.IO.File.Create(@"c:\\Test" + DateTime.Now.ToString("yyyyyMMddHHmmss") + ".pdf"))
{
//create a new file to write to
await response.Content.CopyToAsync(file);
await file.FlushAsync(); // flush back to disk before disposing
}


}

//Render view to string service
public interface IViewRenderService
{
Task<string> RenderToStringAsync(string viewName, ViewDataDictionary viewData);
}
public class ViewRenderService : IViewRenderService
{
private readonly IRazorViewEngine _razorViewEngine;
private readonly ITempDataProvider _tempDataProvider;
private readonly IServiceProvider _serviceProvider;

public ViewRenderService(IRazorViewEngine razorViewEngine,ITempDataProvider tempDataProvider,IServiceProvider serviceProvider)
{
_razorViewEngine = razorViewEngine;
_tempDataProvider = tempDataProvider;
_serviceProvider = serviceProvider;
}

public async Task<string> RenderToStringAsync(string viewName, ViewDataDictionary viewData)
{
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

using (var sw = new StringWriter())
{
var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);

if (viewResult.View == null)
{
throw new ArgumentNullException($"{viewName} does not match any available view");
}


var viewContext = new ViewContext(
actionContext,
viewResult.View,
viewData,
new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
sw,
new HtmlHelperOptions()
);

await viewResult.View.RenderAsync(viewContext);
return sw.ToString();
}
}

}

最佳答案

我在使用 core 2.0 时遇到了同样的错误。问题是 RazorViewEngine 没有像预期的那样使用空的 RouteData 对象;

所以我注入(inject)了IHttpContextAccessor并从中得到了HttpContextRouteData

启动.cs:

  public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IViewRenderService, ViewRenderService>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddMvc();
}

RazorToStringHelper.cs:

  public interface IViewRenderService
{
Task<string> RenderToStringAsync(string viewName, object model);
}

public class ViewRenderService : IViewRenderService
{
private readonly IRazorViewEngine _razorViewEngine;
private readonly ITempDataProvider _tempDataProvider;
private readonly IHttpContextAccessor _httpContextAccessor;

public ViewRenderService(
IRazorViewEngine razorViewEngine,
IHttpContextAccessor httpContextAccessor,
ITempDataProvider tempDataProvider)
{
_razorViewEngine = razorViewEngine;
_tempDataProvider = tempDataProvider;
_httpContextAccessor = httpContextAccessor;
}

public async Task<string> RenderToStringAsync(string viewName, object model)
{
var httpContext = _httpContextAccessor.HttpContext;

var actionContext = new ActionContext(httpContext, httpContext.GetRouteData(), new ActionDescriptor());

var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);

if (viewResult.View == null)
{
throw new ArgumentNullException($"{viewName} does not match any available view");
}

using (var sw = new StringWriter())
{
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = model
};

var viewContext = new ViewContext(
actionContext,
viewResult.View,
viewDictionary,
new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
sw,
new HtmlHelperOptions()
);

await viewResult.View.RenderAsync(viewContext);
return sw.ToString();
}
}

}

关于asp.net-core - Asp.Net Core 2.0 Xunit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45843315/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com