HiPerFreak
unread,Sep 23, 2010, 9:25:06 AM9/23/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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?