How do I moq a method with an out parameter?

376 views
Skip to first unread message

Philippe Lavoie

unread,
Apr 28, 2008, 11:05:38 AM4/28/08
to moq...@googlegroups.com
Hi

I couldn't find an example where something like the following is mocked

public Something SomeMethod(Input input, string foo, out Details bar);

I'd like to moq the result of 'SomeMethod' and also the out parameter bar.

Thanks for your help.

Phil

PS Please answer to my e-mail address directly since I'm not
registered on this list.

Daniel Cazzulino

unread,
Apr 28, 2008, 6:29:23 PM4/28/08
to moq...@googlegroups.com
MoQ does not currently support out or ref arguments.

There's an explicit VS code analysis feature that warns against using out parameters: http://msdn2.microsoft.com/en-us/library/ms182131.aspx

do you own the library you're testing or is it a third party one? have you evaluated making the out value part of the return value?

philipp...@gmail.com

unread,
Apr 29, 2008, 10:05:09 AM4/29/08
to MoQ Discussions
Thanks for the information.

I own the library and I'll try to refactor around this limitation. In
my case, the out parameter is used to add ValidationDetails in case a
method failed. Something like

public ShoppingCart AddToCart(...,out ValidationDetails details);

I think it's a valid use of the out parameter. I'll make my class
statefull to get around this and set a 'validationDetails' property
when error occurs. However, I don't really like that but I prefer it
over modifying what a ShoppingCart is.

Phil

Daniel Cazzulino

unread,
Apr 29, 2008, 12:09:11 PM4/29/08
to moq...@googlegroups.com
maybe the operation should return something different:

public OperationResult<ShoppingCart> AddToCart(....)

class OperationResult<T>
{
   public T Data { get; }
   public ValidationDetails { get; }

philipp...@gmail.com

unread,
Apr 29, 2008, 10:16:54 PM4/29/08
to MoQ Discussions

I was hoping to not use an envelope for all my methods :) In our
solution we try to have an OO interface for the methods inside the UI.
The UI then call a SOA layer for the business logic. The SOA layer has
something akin to the OperationResult that you mention above (single
input, single output). The goal inside the UI was to make the input
and outputs more explicit.

Maybe I'll have to revisit this assumption.

Thanks

Phil

Daniel Cazzulino

unread,
Apr 29, 2008, 10:49:46 PM4/29/08
to moq...@googlegroups.com
maybe you should just throw if something's wrong?

Daniel Cazzulino

unread,
Aug 6, 2009, 2:49:25 PM8/6/09
to Uri Goldstein, moqdisc
The first couple examples in the Moq quickstart show how this feature IS supported in Moq: http://code.google.com/p/moq/wiki/QuickStart

Yes, a lot of things changed in 1+ year since that email :)

/kzu

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


On Tue, Aug 4, 2009 at 12:20 PM, Uri Goldstein <uri.go...@gmail.com> wrote:
Hi Daniel,

Does MoQ not support out or ref arguments as a matter of principal or
because it's technically impractical?

I'm sure many people find out or ref arguments to be a valid design
decision when designing internal libraries for advanced consumers (and
even when implementing public libraries for novices ;) )


Thanks,
Uri


On Apr 29 2008, 1:29 am, "Daniel Cazzulino" <dan...@cazzulino.com>

wrote:
> MoQ does not currently support out or ref arguments.
>
> There's an explicit VS code analysis feature that warns against using out
> parameters:http://msdn2.microsoft.com/en-us/library/ms182131.aspx
>
> do you own the library you're testing or is it a third party one? have you
> evaluated making the out value part of the return value?
>
> On Mon, Apr 28, 2008 at 12:05 PM, Philippe Lavoie <philippe.lav...@gmail.com>
Reply all
Reply to author
Forward
0 new messages