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

时间:2018-12-02 02:30:26

标签: c# asp.net-core integration-testing xunit

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

但是我总是从请求中得到500错误。我还尝试请求在线链接进行测试(https://jsonplaceholder.typicode.com/users),但即使这样我也能获得500。

顺便说一句,当我运行我的api并检查要测试的methot时,它的温度已经达到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);

        }
 }

0 个答案:

没有答案