Jun 8, 2012 at 6:42 AM
Edited Jun 8, 2012 at 6:43 AM
|
Thank you, that does work, but I must say that I find it odd that there are two very different ways of achieving this in MVC controllers and Web API controllers. I think MVC has a far better API for this - as I can do something like this:
HttpContextBase context = Stub<HttpContextBase>();
context.Stub(context => context.Request).Return(Stub<HttpRequestBase>());
FooController foo = new FooController();
foo.ControllerContext = new ControllerContext(context, new RouteData(), foo);
In Web API it must be done like this:
BarController bar = new BarController();
HttpConfiguration configuration = new HttpConfiguration();
HttpRequestMessage request = new HttpRequestMessage();
bar.Request = request;
bar.Request.Properties["MS_HttpConfiguration"] = configuration;
So whats wrong with this:
- Totally different API which is confusing (even with easy setters)
- The ApiController class does have a ControllerContext property like the MVC Controller class, but setting it does not have the same semantics (actually in my case it makes no difference if it is set or not).
- Impossible to mock the HttpRequestMessage object, so when testing it is necessary to build up the entire object by hand. It would have been nice if it was an abstract base class as in MVC.
I would like to be able to do something like this:
HttpRequestMessageBase request = Stub<HttpRequestMessageBase>();
//setup the request stub
BarController bar = new BarController();
bar.ControllerContext = new HttpControllerContext(Stub<HttpConfigurationBase>(), Stub<IHttpRouteData>(), request);
Or something like it.
|