Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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

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
C# NetCore 2.1 TestServer返回500内部服务器错误_C#_Asp.net Core_Integration Testing_Xunit - Fatal编程技术网

C# NetCore 2.1 TestServer返回500内部服务器错误

C# NetCore 2.1 TestServer返回500内部服务器错误,c#,asp.net-core,integration-testing,xunit,C#,Asp.net Core,Integration Testing,Xunit,我正在尝试为我的API编写一个集成测试,我正在尝试在.netCore上使用TestServer并使用xunit 但我总是从请求中得到500个错误。我还尝试请求一个在线链接来测试()但即使这样,我也得到了500 顺便说一句,当我运行我的api并检查我尝试测试的方法是否达到200并且工作得很好 这是我得到的全部信息 测试名称:RepoCore.Test.IntegrationTests.IntegrationTest.queryFromentityyteStatSync 测试全名:RepoCore

我正在尝试为我的API编写一个集成测试,我正在尝试在.netCore上使用TestServer并使用xunit

但我总是从请求中得到500个错误。我还尝试请求一个在线链接来测试()但即使这样,我也得到了500

顺便说一句,当我运行我的api并检查我尝试测试的方法是否达到200并且工作得很好

这是我得到的全部信息


测试名称:RepoCore.Test.IntegrationTests.IntegrationTest.queryFromentityyteStatSync

测试全名:RepoCore.Test.IntegrationTests.IntegrationTest.queryFromentityyteStatSync

测试源:C:\Users\Sercan\Source\repos\RepoCore.API\RepoCore.Test\IntegrationTests\IntegrationTest.cs:第0行

测试结果:失败 测试持续时间:0:00:04645

结果堆栈跟踪
在RepoCore.TEST.IntegrationTests.IntegrationTest.QueryFromentityyteStatSync()中 ---来自引发异常的上一个位置的堆栈结束跟踪---

结果消息:Assert.Equal()失败

预期值:正常

实际值:内部服务器错误


公共类集成测试:IDisposable
{
私有数据上下文_上下文;
私有只读HttpClient\u客户端;
公共集成测试()
{
var configuration=new ConfigurationBuilder().SetBasePath(Path.GetFullPath(@./../../../../../).Build();
var server=newtestserver(new WebHostBuilder().UseStartup().UseConfiguration(配置));
var serviceProvider=new servicecolection().AddEntityFrameworkSqlServer().BuildServiceProvider();
var builder=new DbContextOptionsBuilder();
UseSqlServer(“服务器=xyz;数据库=xyz;用户ID=xyz;密码=1;可信的连接=True;”)。UseInternalServiceProvider(serviceProvider);
_上下文=新的数据上下文(builder.Options,_logContext);
_Migrate();
_client=server.CreateClient();
}
[事实]
公共异步任务QueryFromEntitiyTestAsync()
{
API.Models.Repos newrepo=newapi.Models.Repos{RepoName=“TestName2”,RepoAdress=“Some Street”,IsActive=1};
_context.Repos.Add(newrepo);
_SaveChanges();
var response=wait_client.GetAsync(“/api/Repo/GetRepoById?RepoId=“+newrepo.RepoId”);
//var response=wait_client.GetAsync(“https://jsonplaceholder.typicode.com/users");
Assert.Equal(HttpStatusCode.OK,response.StatusCode);
}
}

您可以在API代码中设置一个断点,如果您调试测试,它将命中该API断点。500基本上是说API代码中发生了一些错误
对客户端没有多大帮助,但您可能有可以检查的日志或者更好的是源代码…@jonscaapi工作正常,没有问题there@JohnB但当我尝试外部链接时,我也得到了同样的500。该链接给出了正确的结果并起作用too@SercanSel当您运行集成测试时,它会在其中的某个地方抛出一个异常。它可能是你如何设置你的测试或任何事情。
 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);

        }
 }