I need to mock current logged in user for testing the following function. This function returns the id of the current logged in user from the Users table in the database.
public static string CurrentUser()
{
string loginuser= System.Web.HttpContext.Current.User.Identity.Name;
if(loginuser != "")
{
using (var cntx = new EdmxEntities())
{
return cntx.Users.Where(s => s.Email == loginuser).Select(x => x.Id).FirstOrDefault();
}
}
return loginuser;
}
(System.Web.HttpContext.Current.User.Identity.Name) This function returns the current logged in username but returns null during testing which throws an Null Reference Exception.