lundi 29 juin 2015

WebApi Unit Test HttpContextHelper with Mock

I have a method I'm trying to mock for unit testing but I'm having difficulty as all the props are read only and mocking fails as it is a sealed class. This method is hung off a controller and I have all that infrastructure in place.

First here is the method I'm trying to test.

public CarResponse(Common.Models.Car car)
{
    string currentAcceptType = HttpContextHelper.Current.Request.Headers["Accept"];

    ....removed for brevity.....

}

I have tried to inject an instantiated object and that would work but my Headers are read only so I can't see how to set the headers to what I want??

HttpRequest cv = new HttpRequest(null, "http://localhost:24244", "/cars/_services/addNewPickup");
//so far so good but then when I want to add my headers every property is read only.
cv.Headers.Add("qewr", "adsf"); //This fails.

MSDN

Ultimately I would be using it as part of the larger fake "session" for testing.

HttpContext.Current = new HttpContext(cv, new HttpResponse(null));

Mocking of HttpRequest fails as it is a sealed class.

So I'm kinda stumped on how to test this unit of code.

Aucun commentaire:

Enregistrer un commentaire