c# - How to properly integration test Web Api controller with IEnumerable results? -


the original question bit incorrect, because problem happens during controller testing how return correct status code when enumeration of ienumerable fails. asp.net webapi

the question how test web api controller returns ienumerable when ienumerable might throw exception during deferred execution?

e.g. have such api endpoint:

public class entitycontroller : apicontroller {     ...     public ihttpactionresult get()     {         return ok(session.query<entity>());     } } 

and corresponding test pass regardless of exception might thrown during query execution.

[test] public async task test() {      var entitycontroller = new entitycontroller(session)                 {                     configuration = new httpconfiguration(),                     request = new httprequestmessage()                 };      var response = await entitycontroller.getall().executeasync(new cancellationtokensource().token);      response.ensuresuccessstatuscode(); } 

however test not pass if casting tolist inside controller itself.

public class entitycontroller : apicontroller {     ...     public ihttpactionresult get()     {         return ok(session.query<entity>().tolist());     } } 

it seems me create integration tests test web api endpoints.

your current test won't ever hit media type formatter or other parts of web api pipeline issues can occur.

i recommend using microsoft.owin.testing nuget package in test project. allow set web api controller in in-memory web server testing cover more of web api pipeline.

judging snippet of code, web api controller not have dependencies on system.web using owin testing may suit needs. of course, not cover specific iis setup or in iis pipeline outside of web api. however, catch exceptions thrown deferred execution of linq query if not use tolist() in controller action.

to use owin , owin testing need follow these steps:

1. add microsoft.owin nuget package web project containing web api controllers wish test.

2. add owin startup class web project:

public class startup {     public virtual void configuration(iappbuilder builder) {         // if want move web api initialization          // , configuration here can.          // otherwise leave method empty     } } 

we override configuration method in test project.

3. add microsoft.aspnet.webapi.owin nuget package test project

4. add microsoft.owin.testing nuget package test project.

5. create test class in test project:

public class testclass {     private class teststartup : startup {         public override void configuration(iappbuilder app) {             // web api, ioc, etc setup here             var config = new httpconfiguration();             config.maphttpattributeroutes();             // ...etc             app.usewebapi(config);         }     }      [test]     public void mytest() {         // arrange         using (var server = testserver.create<teststartup>()) {              // act             var response = server.createrequest("api/someroute/").getasync().result;              // verify             assert.equal(httpstatuscode.internalservererror, response.statuscode);         }     } } 

make sure teststartup inner class derives startup class created in web project in step two, ensure web api pick controllers web project. there other methods, simple , has benefit of being able reuse initialization code actual application if using owin.

6. profit


Comments