I use a third-party UserContext library that only has a bunch of {get;}
public static UserContext Current { get; }
public UserContact CurrentContact { get; }
public string UserContactName { get; }
In my code, it just returns CurrentUser like this:
void DoSomething(){
UserContext Current = UserContext.Current;
}
I have no way to install a new fake unit for unit testing. So, to mock this in my code, I did the following:
Created a subclass that inherits from UserContext and overwrites the current property:
public class UserContextFake : UserContext
{
public new UserContext Current { get; set; }
}
Then create an interface and a wrapper:
public class UserContextWrapper : IUserContext
{
public UserContextWrapper()
{
userContext = UserContext.Current;
}
public UserContextWrapper(UserContext context)
{
userContext = context;
}
private readonly UserContext userContext;
public UserContext Current
{
get { return userContext; }
}
}
Now I can embed userContextWrapper in my classes. I disclose two constructors: one that uses UserContext.Current (third-party library) session material for production code, and one that can receive custom UserContextFake. In IoC, I map IUserContext to UserContext
: CurrentContact, , UserContext (UserContext.CurrentContact)