2

I have been looking at unit testing a ASP.Net WebAPI project. I can obviously test controller Get methods easily in terms of the data it returns however I was looking at testing things like headers.

I have used NancyFX before and this provides a nice clean testing capability that returns a proper HTTP response even though no client/server has been setup.

The alternative I can see in WebAPI is to use HttpServer and HttpConfiguration to run a server in-memory.

If I do this I was wondering if someone knew what the performance of it is. If its not that great then it becomes an integration test unfortunately.

Simple scenario, I want to test the response header content type. Without the in-memory configuration I'm not sure how you test that in WebAPI

1 Answer 1

1

I am running 80 tests in under 1 second. I think performance is adequate.

Note: if you don't host your api tests in the same project as the controllers themselves you will need to override the controller creation. I do this usinga StructureMap dependency resolver which also implements the IHttpControllerActivatorinterface so controllers are resovled from the container rather than the default activator.

Sign up to request clarification or add additional context in comments.

9 Comments

I was looking at something like this strathweb.com/2012/06/… There doesn't seem to be any need for IHttpControllerActivator ???
that article is slightly different. if you want to test the request/response but don't care about the actual controller action then yes it will work. if you want to test the apicontroller actions then the tests either need to be hosted in the api project itself, or you need to override how the controllers are created.
Couldn't you use something like this? gist.github.com/4148674. Maybe I'm not understanding you correctly. Do you have any demo code?
it depends what you want to test. i am assuming you want to test the pipeline by calling the url. in which case you would want to setup an in-memory environment of the host. if that's not what you are after then just write a test around the component without the hosting environment and call the controller action directly. if you can provide the code for what you have already that would be helpful for this discussion.
I see what you're talking about now in terms of IHttpControllerActivator, thanks. Guess I will have to do that for the in-memory server scenario. In terms of getting a response back to read headers etc I think I have to use in-memory unless there is another way? If you're using in-memory and doing 80 requests per second then I think thats good enough to make it a unit test rather than integration
|

Your Answer

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.