Can't mock protected methods that have Nullable<T> parameters

193 views
Skip to first unread message

HiPerFreak

unread,
Sep 23, 2010, 9:25:06 AM9/23/10
to Moq Discussions
Hi,

The following test throws an ArgumentException:

[TestMethod]
public void SetupForNullableTypes() {
new Mock<MoqTest>()
.Protected()
.Setup("TestMethod", new
Nullable<DateTime>(DateTime.Now));
}

private class MoqTest {
protected virtual void TestMethod(Nullable<DateTime> arg) {

}
}

Exception:
System.ArgumentException: Expression of type 'System.DateTime' cannot
be used for parameter of type 'System.Nullable`1[System.DateTime]' of
method 'Void TestMethod(System.Nullable`1[System.DateTime])'

at
System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase
method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)
at
System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodBase
method, ExpressionType nodeKind, ReadOnlyCollection`1& arguments)
at System.Linq.Expressions.Expression.Call(Expression instance,
MethodInfo method, IEnumerable`1 arguments)
at Moq.Protected.ProtectedMock`1.GetMethodCall(MethodInfo method,
Object[] args)
at Moq.Protected.ProtectedMock`1.Setup(String methodName, Object[]
args)

The problem is that if I do the following in C#:

object obj = new Nullable<DateTime>(DateTime.Now);

obj does not hold a boxed Nullable<DateTime> but the real DateTime
value.

Is there currently a way to mock such methods? If not, will this be
fixed sometimes?

Daniel Cazzulino

unread,
Sep 28, 2010, 10:36:43 AM9/28/10
to moq...@googlegroups.com
bummer!

could you create an issue for this so that we can track it?

thanks!

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471


Reply all
Reply to author
Forward
0 new messages