My controller makes use of CreateResponse on the request object. So in order to test my controller I need to set an instance of HttpConfiguration on the HttpRequestMessage because the CreateResponse expects this configuration to be there.
Nowadays, to aid with testing, there are setters for all sorts of properties but on HttpRequestMessage there is only a GetConfiguration method and no apparent setter.
How do I do this?
Use the following code:
...
ThingController controller = new ThingController(... dependencies ...);
// Fake the configuration.
//
var httpConfig = new HttpConfiguration();
controller.Configuration = httpConfig;
// Fake the request.
//
var httpRequest = new HttpRequestMessage(HttpMethod.Get, "http://mstest/things/1");
httpRequest.Properties[HttpPropertyKeys.HttpConfigurationKey] = httpConfig;
controller.Request = httpRequest;
Note the line 2nd from bottom. Sneaky.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With