Unit Testing using Fakes for HttpActionContext objects

Posted by Mahendrabasutkar under ASP.NET Web API on 4/30/2018 | Points: 10 | Views : 499 | Status : [Member] | Replies : 1
I have a Attribute which has the below property set.

actionContext.Request.Properties["isActiveUser"] = true
where actionContext is HttpActionContext actionContext

How do i carry out unit testing using fakes for the same
as using below code HttpRequestMessage() thows error
the type or namespace name 'httprequestmessage' could not be found.

HttpActionContext context = new HttpActionContext();
var headerValue = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", "bzUwkDal=");
var request = new System.Net.Http.HttpRequestMessage();
request.Headers.Authorization = headerValue;
var controllerContext = new HttpControllerContext();
controllerContext.Request = request;
context.ControllerContext = controllerContext;
context.Request.Properties["isActiveUser"] = false;




Responses

Posted by: Jordandavid on: 11/3/2018 [Member] Starter | Points: 25

Up
0
Down
If you are unit testing controller then you should not worry about the execution of any other code outside of the controller. If the changes made to ControllerContext in Authorization attribute are used in the controller code then you should mock or fake the ControllerContext and assign it to Controller and set up its behavior. https://www.brotherprintersupportnumber.com/blog/how-to-fix-brother-printer-offline-in-windows-10/


Mahendrabasutkar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response