mercredi 9 avril 2014

ASP.net WebAPI avec WebAPI.Testing cadre - débordement de pile


We are working to setup automated unit testing on our API calls. We have two projects in our solution. One is an ASP.net WebAPI project, the other is a Microsoft Test project.


Admittedly, we are new to automated unit testing but understand the basic concepts. There appears to be a few open-source tools and frameworks for testing ASP.net WebAPI projects. We are currently working with the WebAPI.Testing framework. There are essentially no docs for it, other than the readme. According to the readme document, using the tool is super simple and straight forward, but it's not working for us.


Here is our test method:


[TestMethod]
public void TestMethod1()
{
var config = new HttpConfiguration();

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

Browser browser = new Browser(config);

var response = browser.Get("/api/activities/", (with) =>
{
with.Header(SessionIdHeaderKeyName, "e999e767-5b63-4cc0-b2be-34842eb3adf0");
with.Header("Accept", "application/json");
with.HttpRequest();
});

Assert.Equals(HttpStatusCode.OK, response.StatusCode);
}

We chose this framework because it allows us to set headers before calling the request. The code executes fine with no exceptions, however all we get is a 404 Not Found, regardless of that we specify in the path (/api/activities). Honestly, I don't see how this could work at all, since there doesn't seem to be any reference to my WebAPI project. So how could the test framework possibly know anything about what the route "/api/activities" resolves to or even where to find the corresponding controller.


Does anyone have any experience with this or other frameworks which would test our ASP.net WebAPI controller and allow us to set headers before the request?


Can someone tell me what I might be doing wrong here?


I emailed the author of the WebAPI.Testing framework and he was kind enough to respond and tell me that I needed to include a reference to my API project ("Your test project will need to reference your API project then hopefully it will find the routes."). However, simply adding a reference to my in my test project to my WebAPI project didn't help. If anyone else has any suggestions, I would greatly appreciate them.


I have included a reference to my WebAPI project in my Test project. I also updated the code to include the HttpConfiguration setup. I have verified the route path and the controller action are correct. However, I still get a 404 when I run my test. I don't understand how this code knows where to find the controller and wire it up.




The first thing is that your test project essentially becomes a kind of self hosted web api project when you include the web.api testing library (and hence web api itself).


This means that you don't get anything scaffolded to help you set up the routing etc like you would in a proper web api project... this has to come from the project under test along with controllers etc.


So as you've noticed the reference is the first part... but it sounds like you are missing a call to the webapiconfig bits from your api project that I just mentioned... and the 404 is because there are no routes set up until you do so


You'll need to do this in your test setup/initialise method, but then hopefully it should work. I'm not at my pc at the moment so no sample code but you can look at the webapi integration tests in http://github.com/roysvork/linqtoquerystring.



We are working to setup automated unit testing on our API calls. We have two projects in our solution. One is an ASP.net WebAPI project, the other is a Microsoft Test project.


Admittedly, we are new to automated unit testing but understand the basic concepts. There appears to be a few open-source tools and frameworks for testing ASP.net WebAPI projects. We are currently working with the WebAPI.Testing framework. There are essentially no docs for it, other than the readme. According to the readme document, using the tool is super simple and straight forward, but it's not working for us.


Here is our test method:


[TestMethod]
public void TestMethod1()
{
var config = new HttpConfiguration();

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

Browser browser = new Browser(config);

var response = browser.Get("/api/activities/", (with) =>
{
with.Header(SessionIdHeaderKeyName, "e999e767-5b63-4cc0-b2be-34842eb3adf0");
with.Header("Accept", "application/json");
with.HttpRequest();
});

Assert.Equals(HttpStatusCode.OK, response.StatusCode);
}

We chose this framework because it allows us to set headers before calling the request. The code executes fine with no exceptions, however all we get is a 404 Not Found, regardless of that we specify in the path (/api/activities). Honestly, I don't see how this could work at all, since there doesn't seem to be any reference to my WebAPI project. So how could the test framework possibly know anything about what the route "/api/activities" resolves to or even where to find the corresponding controller.


Does anyone have any experience with this or other frameworks which would test our ASP.net WebAPI controller and allow us to set headers before the request?


Can someone tell me what I might be doing wrong here?


I emailed the author of the WebAPI.Testing framework and he was kind enough to respond and tell me that I needed to include a reference to my API project ("Your test project will need to reference your API project then hopefully it will find the routes."). However, simply adding a reference to my in my test project to my WebAPI project didn't help. If anyone else has any suggestions, I would greatly appreciate them.


I have included a reference to my WebAPI project in my Test project. I also updated the code to include the HttpConfiguration setup. I have verified the route path and the controller action are correct. However, I still get a 404 when I run my test. I don't understand how this code knows where to find the controller and wire it up.



The first thing is that your test project essentially becomes a kind of self hosted web api project when you include the web.api testing library (and hence web api itself).


This means that you don't get anything scaffolded to help you set up the routing etc like you would in a proper web api project... this has to come from the project under test along with controllers etc.


So as you've noticed the reference is the first part... but it sounds like you are missing a call to the webapiconfig bits from your api project that I just mentioned... and the 404 is because there are no routes set up until you do so


You'll need to do this in your test setup/initialise method, but then hopefully it should work. I'm not at my pc at the moment so no sample code but you can look at the webapi integration tests in http://github.com/roysvork/linqtoquerystring.


0 commentaires:

Enregistrer un commentaire