I have the following method:
public CustomObect MyMethod()
{
var lUser = GetCurrentUser();
if (lUser.HaveAccess)
{
//One behavior
}
else
{
//Other behavior
}
//return CustomObject
}
I want to mock IMyInterface.GetCurrentUser
, so that while calling MyMethod
I could get to one of the code paths to check it. How to do that with Moq?
I'm doing the following thing:
var moq = new Mock<IMyInterface>();
moq.Setup(x => x.GetCurrentUser()).Returns(lUnauthorizedUser);
//act
var lResult = moq.Object.MyMethod();
But for some reason lResult
is always null
, and when I'm trying to get into MyMethod
in debug, I'm always skipping to the next statement.
lUnauthorizedUser
initialized? I would imagine you would want something likemoq.Setup(x => x.GetCurrentUser()).Returns(new User() { HaveAccess = false });
– Subjunctivem setting it in the above code, just didn
t pasted it to keep the code short. – Unearned