MVC Is it good to access HttpContext.Current.User in a model and how to test

in one of my model classes, I have a property:

public virtual bool OkToChangeApprovedForPayment
        {
            get
            {
                return ExportedDateTime == null && PMApproved == true && HttpContext.Current.User.IsInRole(Resources.Roles.VectorOpsAdmin);
            }
        }

As you can see, one of the conditions is to check the role the user is in. Is it possible to access the HttpContext in the model as follows? If this is not the best way to do this?

If this is normal, how would you unit test it? How do you replace the call with an HttpContext?

+3
source share
1 answer

This is safe (if your model runs in the context of the query being executed, of course).

However, it will be difficult to verify. You can inject an instance of HttpContextBase into a model that is more suitable for testing.
+2
source

All Articles