HTTPEXTENSION METHOD
public static class HttpRequestMessageExtensions
{
public static HttpResponseMessage CreateInvalidJsonResponse(this HttpRequestMessage request)
{
return request.CreateResponse<InvalidJson>(HttpStatusCode.BadRequest, new InvalidJson());
}
}
TEST
[TestFixture]
public class ShowControllerTest
{
private ShowController _showController;
private IShowService _showService;
[SetUp]
public void Setup()
{
_showService = MockRepository.GenerateMock<IShowService>();
_showController = new ShowController(_showService);
var httpRequest = new HttpRequestMessage(HttpMethod.Post, "http://localhost/");
var config = new HttpConfiguration();
httpRequest.Properties[HttpPropertyKeys.HttpConfigurationKey]= config;
_showController.Request = httpRequest;
_showController.Configuration = config;
var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary(new { controller = "show" }));
_showController.Request.Properties[HttpPropertyKeys.HttpRouteDataKey]= routeData;
}
[Test]
public void ProcessShows_Should_Return_InvalidJsonResponse_When_Request_Is_Invalid()
{
//Arrange
var request = new GenericRequest<Show>();
//Act
request = null;
_showController.ProcessShows(request);
//Assert
}
}
CONTROLLER
[RoutePrefix("")]
public class ShowController : ApiController
{
private readonly IShowService _showService;
public ShowController(IShowService showService)
{
_showService = showService;
}
[Route("")]
[HttpPost]
public HttpResponseMessage ProcessShows([FromBody] GenericRequest<Show> request)
{
//Return if invalid json
if (request == null)
return Request.CreateInvalidJsonResponse();
}
I have searched around and people are saying to add HttpPropertyKeys.HttpConfigurationKey which i have but still it isn't working .
throws The request does not have an associated configuration object or the provided configuration was null.
Aucun commentaire:
Enregistrer un commentaire