Gotcha. I think.
So for this class (I simplified it to be in the same class, but it obviously works with other classes too):
public class Provider
{
Dictionary<Type, object> _dictionaryOfTypeToInstance;
public object GetService(System.Type type)
{
// should throw because it's null
return _dictionaryOfTypeToInstance[type];
}
public void SomeCallingCode()
{
SomeUserType obj = (SomeUserType)GetService(typeof(SomeUserType));
}
}
You have these options three:
1. If you don't care about the type at all, and plan to work according to sequence, you can use this:
[TestMethod]
public void Regardless_Of_Arguments_Just_Based_On_Call_Sequence()
{
Provider provider = new Provider();
Isolate.WhenCalled(() => provider.GetService(null)).WillReturn(new SomeUserType());
provider.SomeCallingCode();
}
(note in all options, the fact that the test passes means that the faking succeeded).
2. If you want to be specific about the type in the argument, use this:
[TestMethod]
public void By_Exact_Argument()
{
Provider provider = new Provider();
Isolate.WhenCalled(() => provider.GetService(typeof(SomeUserType)))
.WithExactArguments().WillReturn(new SomeUserType());
provider.SomeCallingCode();
}
3. If you want to dynamically control this, you can use:
[TestMethod]
public void Dynamically_Control_Return_Value()
{
Provider provider = new Provider();
Isolate.WhenCalled(() => provider.GetService(null)).DoInstead(callContext =>
{
if (callContext.Parameters[0].GetType() == typeof(SomeUserType))
{
return new SomeUserType();
}
return null;
});
provider.SomeCallingCode();
}
Also for 3, in version 7.3, there's the ability to continue with the original method implementation, so that may be useful too:
callContext.WillCallOriginal();
Does any of these seem to resolve it?