Today I would like to quickly present you how to unit test Web.API based OData Controllers the easy way. On biz.dfch.CS.Examples.Odata you find a complete code example and some accompanying information.
The idea is based on the following information (and slightly enhanced):
* Unit Testing Web API OData Services – Part 1
* Unit Testing Web API OData Services – Part 2
* Unit Testing Web API OData Services – Part 3
* OWIN Self-Hosted Test Server for Integration Testing of OData and Web API
In short, the unit test us the WebApiConfig
startup code from the real Web.API project and host it in a Katana TestServer. Then on the client is a service reference is used to abstract the HTTP/JSON/XML logic of the Web Controllers. So the client can then test as easy as this:
[TestMethod] public void GetThingByNameReturnsThing() { // Arrange var container = new biz.dfch.CS.Examples.Odata.Client.Utilities.Container(uri); var name = "theThing"; // Act var entity = container.Things.Where(e => e.Name.Equals(name)).First(); // Assert Assert.IsNotNull(entity); Assert.AreEqual(name, entity.Name); }
Check the README for further information.