how to Setup method with params?

1,480 views
Skip to first unread message

Steve

unread,
Feb 18, 2009, 5:41:39 PM2/18/09
to Moq Discussions
This is the signature of the method i need to mock out:

bool ExecuteBoolean(string cmdText, TransactionManager
transaction, params IDataParameter[] parameters)

I tried:
(1) mockContext.Setup(x => x.ExecuteScalarBoolean(It.IsAny<string>(),
null, null)).Throws(new SystemException());
(2) mockContext.Setup(x => x.ExecuteScalarBoolean(It.IsAny<string>(),
null)).Throws(new SystemException());
(3) mockContext.Setup(x => x.ExecuteScalarBoolean(It.IsAny<string>(),
null, It.IsAny<IDataParameter[]>)).Throws(new SystemException());

None of these worked. I'm sure i'm doing something dumb....

andreister

unread,
Feb 19, 2009, 9:07:16 AM2/19/09
to Moq Discussions
I'm afraid you'd need to mock every call separately

mockContext.Setup(x => x.ExecuteScalarBoolean(It.IsAny<string>(),
null, It.IsAny<IDataParameter>()).Throws(new SystemException())

this will mock "foo.ExecuteScalarBoolean(str, param)" call

mockContext.Setup(x => x.ExecuteScalarBoolean(It.IsAny<string>(),
null, It.IsAny<IDataParameter>(), It.IsAny<IDataParameter>().Throws
(new SystemException())

this will mock "foo.ExecuteScalarBoolean(str, param1, param2)" one,
and so on.
Message has been deleted

Daniel Cazzulino

unread,
Feb 19, 2009, 1:40:49 PM2/19/09
to moq...@googlegroups.com
This is fixed in 3.0.
Are you using 2.6.x?

/kzu

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


On Thu, Feb 19, 2009 at 4:08 PM, Steve <steve.bur...@gmail.com> wrote:

I tried this (with one It.IsAny<IDataParameter>().  I know this
particular call uses 1 param parameter).  I turned Strict on and MOQ
throws an exception "All invocations on the mock must have a
corresponding setup..".

Brad Stiles

unread,
Feb 19, 2009, 1:49:26 PM2/19/09
to moq...@googlegroups.com
> This is the signature of the method i need to mock out:
>
> bool ExecuteBoolean(string cmdText, TransactionManager
> transaction, params IDataParameter[] parameters)
>
> I tried:
> (1) mockContext.Setup(x => x.ExecuteScalarBoolean(It.IsAny<string>(),
> null, null)).Throws(new SystemException());

Being new to Moq, take what I say with a grain of salt, but could you
not do something like:

mockContect.Expect(x => x.ExecuteBoolean(It.IsAny<string>(),
It.IsAny<TransactionManager>(), It.IsAny<IDataParameter[]>())
.Callback<string, TransactionManager,
IDataParameter[]>((s, t, p) => CheckParmsAndDoSomething()):

Note that I didn't test any of that, so YMMV.

That aside, what are you trying to do here? If you are trying to test
what the caller of ExecuteBoolean will do when it gets a
SystemExeception back, I don't think you need to go to such lengths.
Just do a ".Throws(...), without checking the parms. After all,
you're assuming that input is invalid and want to test the caller's
reaction to that.

If on the other hand, you are trying to check to make sure the caller
doesn't send null, just do the .Expect/.Setup, without a .Throws or
.Callback and Moq should take care of raising an exception if one of
the parms is null, since it won't meet the expectation.

/bs

Reply all
Reply to author
Forward
0 new messages