In my MVC project, I am building unit tests for a controller to return a view.
This controller's action result builds a viewmodel, and this viewmodel's constructor calls
HttpContext.Current.User.Identity.NameWithoutDomain().ToUpper();
Where NameWithoutDomain
is an extension method of IIdentity
.
Whenever I run it, I keep getting "the object cannot be found" and I'm curious how can I Mock this appropriately for unit testing?
Extension method:
public static class SystemWebExtension
{
public static string NameWithoutDomain(this IIdentity identity)
{
return identity.Name.Split('\\').Last();
}
}
Model constructor:
public Model()
{
this.PreparedByUser = HttpContext.Current.User.Identity.NameWithoutDomain().ToUpper();
this.AuthorizedBy = true;
this.AuthorizedByUser = HttpContext.Current.User.Identity.NameWithoutDomain().ToUpper();
this.IssueCredit = true;
this.CreateUser = string.IsNullOrEmpty(this.CreateUser) ? Environment.UserName.ToLower() : this.CreateUser;
this.CreateDate = this.CreateDate.HasValue ? this.CreateDate : DateTime.Now;
this.UpdateUser = string.IsNullOrEmpty(this.CreateUser) ? null : Environment.UserName.ToLower();
this.UpdateDate = this.CreateDate.HasValue ? DateTime.Now : (DateTime?) null;
}