aspnet / AspNetKatana

Microsoft's OWIN implementation, the Katana project
Apache License 2.0
959 stars 331 forks source link

NullRef when starting TestServer in XUnit dotnetcore project #196

Closed SamVanhoutte closed 5 years ago

SamVanhoutte commented 6 years ago

Hello,

I created an XUnit test project, referencing the following packages:

I don't have an app.config, nor project.json file in my test project and I reference several dotnetstandard libraries in the same solution. I created the following TestStartup.cs file:

    public class TestStartup
    {
        public void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration
            {
                IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always
            };
            //config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            app.UseWebApi(config);

        }
    }

And I start the TestServer with the following line in my Xunit test:

       public async Task TestAuthorization(bool addQueryString, string name, string secret, HttpStatusCode expectedCode)
        {
            using (var server = TestServer.Create<TestStartup>())
            {
                var queryString = addQueryString ? $"?{name}={secret}" : "";
                var response = await server.CreateRequest($"/events/test{queryString}")
                    .And(message => { message.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(TestArtifacts.SubscriptionValidationEvent)); })
                    .PostAsync();
                Assert.Equal(expectedCode, response.StatusCode);
            }
        }

However, I get the following exception and stack trace, when running the test. Any idea?

System.NullReferenceException : Object reference not set to an instance of an object. at Microsoft.Owin.Hosting.Utilities.SettingsLoader.FromConfigImplementation..ctor() at Microsoft.Owin.Hosting.Utilities.SettingsLoader.b__0() at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Func`1 valueFactory) at Microsoft.Owin.Testing.TestServer.Configure[TStartup](StartOptions options) at Microsoft.Owin.Testing.TestServer.Create[TStartup]()

Tratcher commented 6 years ago

These projects aren't supported on dotnetcore. Should you be using the ASP.NET Core versions instead?

Tratcher commented 6 years ago

Note, it's probobly the strong name reference here that's broken: https://github.com/aspnet/AspNetKatana/blob/b259478b40444b06fd89395900b5c2aa32a4ebeb/src/Microsoft.Owin.Hosting/Utilities/SettingsLoader.cs#L132

SamVanhoutte commented 6 years ago

I added a reference to the following package: System.Configuration.Abstractions and that seems to work now. So, I have my tests successfully running.