Reputation: 20129
I'm using Moq for some C# testing. For some reason, I'm getting a null back instead of a string. I'm trying to test
public void Foo(IData data){
_value = data.GetValue<T>(someString);
}
interface IData
{
T GetValue<T>(string someString);
}
and in my test code I have
Mock<IData> dataMock = new Mock<IData>();
dataMock.Setup(x => x.GetValue<string>(It.IsAny<string>())).Returns("blah");
Foo(dataMock.Object);
But when I step through, _value
gets assigned null. Shouldn't it be assigned "blah"
?
Upvotes: 1
Views: 255
Reputation: 156459
Most likely the generic T
parameter in your call to GetValue
is not string
, so the Setup condition is not matched. By default Moq will return default values (null
in this case) from method calls that haven't been explicitly Setup, unless you tell it to be "strict."
// tell Moq to throw an exception if someone calls a method that you haven't `Setup`
Mock<IData> dataMock = new Mock<IData>(MockBehavior.Strict);
Upvotes: 1