2

I am trying to understand how the self host configuration based Integration Tests are running.

In the code below, Should I be registering my config with the WebApiConfig. Registering or not seems to make no difference.

Is the full pipeline really being tested or is this an illusion? Since, If I am not using the config and the routes defined in my API instead declaring my own as I have done here, I am probably just not testing the full pipleine.

Is there any othere way of testing the api completely. The code below is testing a lot of things besides just my pipeline(like the client, SelfHosting etc..). This seems like an overkill to me. Any ideas ?

var config = new HttpSelfHostConfiguration("http://localhost:9090/");
 config.Routes.MapHttpRoute("Default", "{api}/{controller}/{id}", new { id = RouteParameter.Optional });
 config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
 MyApiProject.WebApiConfig.Register(config);
 using (var server = new HttpSelfHostServer(config))
 {
 server.OpenAsync().Wait();
 using (var client = new HttpClient())
 {
 using (var response = client.PostAsync("http://localhost:9090/api/login",
 new FormUrlEncodedContent(new List<KeyValuePair<string,string>> { new KeyValuePair<string, strin("Foo","Bar)}), CancellationToken.None).Result)
 {
 Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
 }
 using (var response = client.GetAsync("http://localhost:9090/api/login").Result)
 {
 Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
 }
 }
 server.CloseAsync().Wait();
 }
asked Mar 7, 2013 at 11:33

1 Answer 1

4

If you just want to test your controllers, you can write more targeted unit tests to test them. If you want to test the full pipeline your code looks fine except that instead of using a selfhost, you can just use HttpServer saving the network overhead. Also, if you are testing the full pipeline it is better to use the routes that you have in your actual app rather than adding a new route as that would be testing routing as well.

Also, refer to this blog post by Youssef for some ideas on testing your web APIs.

answered Mar 7, 2013 at 18:06

2 Comments

I am not looking at unit tests, they are separate and targeted as you said. What would be the correct way of using the configuration I have specified in my api in the tests I have mentioned in the Question ?
You can just call the same code i.e add all your routes and other stuff. I believe MyApiProject.WebApiConfig.Register(config); is doing that only.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.