Reputation: 5
I am trying to unit test following code
var test = WebUtil.GetSessionValue("test") as ViewResult;
And I have setup my test like this:
var session = new Moq.Mock<WebUtil>();
session.SetUp(s => s.GetSessionValue).Returns();
I am having a hard time mocking WebUtil and set up value for GetSessionValue
Upvotes: 0
Views: 193
Reputation: 2708
You cannot mock WebUtil
static methods. You need an abstraction but in this particular case introducing smth like IWebUtil
may look too awkward.
A better solution would be to use an overloaded version of the GetSessionValue
method which receives HttpContextBase
as a parameter:
public static object GetSessionValue(string key, HttpContextBase context) { ... }
The HttpContextBase
is a new well mockable version of the old static HTTP context. You can find how to mock it here. Mocking Session property should be straightforward after that.
Upvotes: 1