I am trying to write a unit test which tests a method that uses HttpContext.GetGlobalResourceObject() and I would like to mock it using Moq.
Let's say we're testing this method:
public List<DctmGridColumn> GetDctmColumnsMandatory()
{
List<DctmGridColumn> metadataFields = new List<DctmGridColumn>
{
new DctmGridColumn(HttpContext.GetGlobalResourceObject("SharePoint.Service", "DctmGridColumn_DispName_r_object_id").ToString()),
new DctmGridColumn(HttpContext.GetGlobalResourceObject("SharePoint.Service", "DctmGridColumn_DispName_object_name").ToString()),
new DctmGridColumn(HttpContext.GetGlobalResourceObject("SharePoint.Service", "DctmGridColumn_DispName_r_modify_date").ToString()),
new DctmGridColumn(HttpContext.GetGlobalResourceObject("SharePoint.Service", "DctmGridColumn_DispName_r_version_label").ToString())
};
return metadataFields;
}
And this is my test:
[Test]
public void TestGetDctmColumnsMandatory_IsNotNull()
{
var columns = _viewDefinitionOperations.GetDctmColumnsMandatory();
Assert.IsNotNull(columns);
}
How can I mock HttpContext? I've been googling the whole day, and I found one or two examples using a mocking framework, the rest creates their own mocks. I've tried to use the approach marked as answer in Setting HttpContext.Current.Session in a unit test, but then I'll have to "replace any calls to HttpContext.Current with HttpContextFactory.Current and have access to the same methods." I'm not that comfortable with that solution.
I'm fairly new to mocking, but I guess there has to be an easier way? I just want to direct all my calls to HttpContext from the unit tests to my mock class. Is that to much to ask? :)