c# - NetCore 2.1 TestServer 返回 500 内部服务器错误
问题描述
我正在尝试为我的 API 编写集成测试,并且我正在尝试在 .netCore 上使用 TestServer 并使用 xunit。
但我总是从请求中得到 500 错误。我还尝试请求在线测试链接(https://jsonplaceholder.typicode.com/users),但即使这样我也得到了 500。
顺便说一句,当我运行我的 api 并检查我要测试的方法是否达到 200 并且运行良好时。
这是我得到的所有信息。
测试名称:RepoCore.TEST.IntegrationTests.IntegrationTest.QueryFromEntitiyTestAsync
测试全名:RepoCore.TEST.IntegrationTests.IntegrationTest.QueryFromEntitiyTestAsync
测试源:C:\Users\Sercan\source\repos\RepoCore.API\RepoCore.TEST\IntegrationTests\IntegrationTest.cs:第 0 行
测试结果:失败 测试持续时间:0:00:04,645
结果 StackTrace:
在 RepoCore.TEST.IntegrationTests.IntegrationTest.QueryFromEntitiyTestAsync() --- 从先前抛出异常的位置结束堆栈跟踪---
结果消息:Assert.Equal() 失败
预期:好的
实际:InternalServerError
public class IntegrationTest : IDisposable
{
private DataContext _context;
private readonly HttpClient _client;
public IntegrationTest()
{
var configuration = new ConfigurationBuilder().SetBasePath(Path.GetFullPath(@"../../../../../../")).Build();
var server = new TestServer(new WebHostBuilder().UseStartup<Startup>().UseConfiguration(configuration));
var serviceProvider = new ServiceCollection().AddEntityFrameworkSqlServer().BuildServiceProvider();
var builder = new DbContextOptionsBuilder<DataContext>();
builder.UseSqlServer("Server=xyz;Database=xyz; User ID=xyz;Password=1; Trusted_Connection=True;").UseInternalServiceProvider(serviceProvider);
_context = new DataContext(builder.Options, _logContext);
_context.Database.Migrate();
_client = server.CreateClient();
}
[Fact]
public async Task QueryFromEntitiyTestAsync()
{
API.Models.Repos newrepo = new API.Models.Repos { RepoName = "TestName2", RepoAdress = "Some Street", IsActive = 1 };
_context.Repos.Add(newrepo);
_context.SaveChanges();
var response = await _client.GetAsync("/api/Repo/GetRepoById?RepoId=" + newrepo.RepoID);
//var response = await _client.GetAsync("https://jsonplaceholder.typicode.com/users");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
解决方案
推荐阅读
- mysql - SQL - 基于一个字段替换条目
- python - 在 Python Anywhere Bash 控制台中安装 PyAudio
- docker - 容器工作,但本地主机说无法连接
- firebase - 如何从 Flutter 引用 Firestore 中的文档并在应用程序的不同位置获取该数据
- database - 错误:在表“S2”上插入或更新违反外键约束“S2_customer_id_fkey”
- python - Linux 上的 Python:如何*立即*将文件移动到 USB 以避免操作系统不同步和 USB 突然被拔出的问题
- node.js - 在 Nodejs 中定期检索访问密钥的代码片段
- java - 在 Java 中的数组中查找字符串
- c++ - 强制非推断上下文 - type_identity 等
- javascript - 从电子js中的剪贴板获取屏幕截图作为arrayBuffer