You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public interface ITokenProvider
{
bool ValidateToken(string token, out Dictionary<string, string> claims);
}
We have also a test with same code on setup (test constructor):
Mock<ITokenProvider> tokenProvider = new Mock<ITokenProvider>();
tokenProvider
.Setup(c => c.ValidateToken(It.IsAny<string>(), out It.Ref<Dictionary<string, string>>.IsAny))
.OutCallback((string token, out Dictionary<string, string> dic) =>
{
dic = new Dictionary<string, string> {{"uid", token}};
})
.Returns(true);
Everything was fine with Moq 3.9.0.
Sometime ago we updated to last version 4.13.1 and our setup was broken with stacktrace:
System.NullReferenceException : Object reference not set to an instance of an object.
at Moq.MoqExtensions.OutCallbackInternal[TMock,TReturn](ICallback'2 mock, Object action)
at Moq.MoqExtensions.OutCallback[TMock,TReturn,T1,TOut](ICallback'2 mock, OutAction'2 action)
at Test..ctor()
When I saw to OutCallBackInternal method then I figured out that problem is in GetDeclaredMethod("SetCallbackWithArguments"), which returns null in this reflected code:
private static IReturnsThrows<TMock, TReturn> OutCallbackInternal<TMock, TReturn>(
ICallback<TMock, TReturn> mock,
object action)
where TMock : class
{
((IFluentInterface) mock).GetType().GetTypeInfo().Assembly.GetType("Moq.MethodCall").GetTypeInfo().GetDeclaredMethod("SetCallbackWithArguments").Invoke((object) mock, new object[1]
{
action
});
return mock as IReturnsThrows<TMock, TReturn>;
}
Same thing happened also with nearest upper version: 4.10.0.
Is I do anything wrong?
The text was updated successfully, but these errors were encountered:
If you choose to reflect over Moq internals, you're basically on your own, there's no guarantee that internals stay the same across versions... they're internals, after all, and not a public API.
That being said, assuming that OutCallback does the same thing as Callback, for specifically for callbacks having certain out parameters, I suggest you simply use Moq 4.8+'s public API:
We have an interface:
We have also a test with same code on setup (test constructor):
Everything was fine with Moq 3.9.0.
Sometime ago we updated to last version 4.13.1 and our setup was broken with stacktrace:
When I saw to OutCallBackInternal method then I figured out that problem is in GetDeclaredMethod("SetCallbackWithArguments"), which returns null in this reflected code:
Same thing happened also with nearest upper version: 4.10.0.
Is I do anything wrong?
The text was updated successfully, but these errors were encountered: