ASP.Net核心集成测试(ASP.Net Core Integration Testing)

编程入门 行业动态 更新时间:2024-10-28 08:29:00
ASP.Net核心集成测试(ASP.Net Core Integration Testing)

我正在努力使用ASP.Net Core RC2进行任何类型的集成测试。 我已经创建了一个基本的Web项目,它在浏览器中运行良好,显示了预期的默认页面。 然后我使用以下测试代码添加了一个新类(在同一个项目中):

[TestClass] public class HomeControllerTests { private HttpClient client; [TestInitialize] public void Initialize() { // Arrange var host = new WebHostBuilder() .UseEnvironment("Development") .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>(); TestServer server = new TestServer(host); client = server.CreateClient(); } [TestMethod] public async Task CheckHomeIndex() { string request = "/"; var response = await client.GetAsync(request); response.EnsureSuccessStatusCode(); Assert.IsTrue(true); } }

测试未通过,但有以下例外:

未找到“索引”视图。 搜索了以下位置: /Views/Home/Index.cshtml /Views/Shared/Index.cshtml

我在这个阶段使用MSTest而不是xUnit。 不幸的是,将ContentRoot更改为“重复”问题中建议的内容并不能解决问题。

有没有人进行集成测试? 我试过调整WebHostBuilder设置,但没有运气。

I'm struggling to get any sort of integration tests working with ASP.Net Core RC2. I have created a basic web project which runs fine in the browser showing the default page as expected. I then added a new class (in the same project) with the following test code:

[TestClass] public class HomeControllerTests { private HttpClient client; [TestInitialize] public void Initialize() { // Arrange var host = new WebHostBuilder() .UseEnvironment("Development") .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>(); TestServer server = new TestServer(host); client = server.CreateClient(); } [TestMethod] public async Task CheckHomeIndex() { string request = "/"; var response = await client.GetAsync(request); response.EnsureSuccessStatusCode(); Assert.IsTrue(true); } }

The test does not pass, with the following exception:

The view 'Index' was not found. The following locations were searched: /Views/Home/Index.cshtml /Views/Shared/Index.cshtml

I'm using MSTest as opposed to xUnit at this stage. Changing the ContentRoot to that suggested in the "duplicate" question unfortunately does not resolve the issue.

Has anyone got integration tests working? I've tried tweaking the WebHostBuilder settings but with no luck.

最满意答案

我写了一篇关于集成测试的博客文章(不包括MVC),而“Snow Crash”则发表了类似问题的评论。 他们使用以下代码解决了“未找到”错误:

var path = PlatformServices.Default.Application.ApplicationBasePath; var setDir = Path.GetFullPath(Path.Combine(path, <projectpathdirectory> )); var builder = new WebHostBuilder() .UseContentRoot(setDir) .UseStartup<TStartup>();

博客文章介绍了使用ASP.NET Core中的xUnit和TestServer进行集成测试的简介 。

I wrote a blog post on integration testing (not covering MVC), and 'Snow Crash' commented with a similar problem. They solved the 'not found' error with the following code:

var path = PlatformServices.Default.Application.ApplicationBasePath; var setDir = Path.GetFullPath(Path.Combine(path, <projectpathdirectory> )); var builder = new WebHostBuilder() .UseContentRoot(setDir) .UseStartup<TStartup>();

The blog post is here Introduction to integration testing with xUnit and TestServer in ASP.NET Core.

更多推荐

本文发布于:2023-07-16 12:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128511.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:核心   测试   Net   ASP   Integration

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!