我很难让任何类型的集成测试与ASP.Net核心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设置,但没有运气。
发布于 2016-06-15 19:34:00
我写了一篇关于集成测试的博文(未涉及MVC),“雪花崩溃”也有类似的问题。他们用以下代码解决了“not”错误:
var path = PlatformServices.Default.Application.ApplicationBasePath;
var setDir = Path.GetFullPath(Path.Combine(path, <projectpathdirectory> ));
var builder = new WebHostBuilder()
.UseContentRoot(setDir)
.UseStartup<TStartup>();博客文章在这里,xUnit和TestServer在ASP.NET内核中的集成测试介绍。
https://stackoverflow.com/questions/37831061
复制相似问题