OUT or REF parameters in C#

295 views
Skip to first unread message

urbadave

unread,
Feb 28, 2011, 5:05:48 PM2/28/11
to NSubstitute
When a mock contains a method like this:

bool OpWorkedOrError(string aParam, out string optionaErrorMsg);

How do you handle the return value of mock in NSubstitute?

I can do the following:

var subForIt = Substitute.For<IInterfaceWithOutParam>();
string optErrorMsg = string.Empty;
subForIt.OpWorkedOrError("0", out optErrorMsg).Returns(false);

But how do I set a value for the out parameter (optErrorMsg)?

urbadave

David Tchepak

unread,
Feb 28, 2011, 6:24:04 PM2/28/11
to nsubs...@googlegroups.com
Hi urbadave,

Sorry, we don't have support for setting out params in v1.0.
It's on the list for next release.

Regards,
David


--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To post to this group, send email to nsubs...@googlegroups.com.
To unsubscribe from this group, send email to nsubstitute...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nsubstitute?hl=en.


PRUIVO.PT

unread,
Jun 2, 2011, 2:22:50 PM6/2/11
to nsubs...@googlegroups.com
Hi David, can you update me on this ? I'm implementing tests for a big project and I'm short on the methods that implement out parameters.

Thanks!

David Tchepak

unread,
Jun 2, 2011, 5:04:43 PM6/2/11
to nsubs...@googlegroups.com
Not done yet I'm afraid.

Would it be sufficient to just have something like this?

subForIt.OpWorkedOrError("0", out optErrorMsg).Returns(x => { x[1] = "this output"; return false; });

We're looking at a (slightly) nicer syntax for this, but in the meantime would that work for you (and for anyone else interested in out/refs?))?

Regards,
David

On Fri, Jun 3, 2011 at 4:22 AM, PRUIVO.PT <pruivo.pt@gmail.com> wrote:
Hi David, can you update me on this ? I'm implementing tests for a big project and I'm short on the methods that implement out parameters.

Thanks!

--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nsubstitute/-/OEtmVUd4dHlXTFFK.

PRUIVO.PT

unread,
Jun 3, 2011, 5:26:38 AM6/3/11
to NSubstitute
I will try it out to see if it behaves well with more complex objects
has the out parameter.
I'll get back to you with my findings.

Cheers.

David Tchepak

unread,
Jun 3, 2011, 5:32:42 AM6/3/11
to nsubs...@googlegroups.com
Ok, give me 48 hours and I'll push this up. 

I can email you the build if you don't want to d/load the source. Just email me off-list.

Cheers,
David

David Tchepak

unread,
Jun 4, 2011, 11:43:49 AM6/4/11
to nsubs...@googlegroups.com
For anyone interested I've pushed up a very rudimentary implementation of this as promised:


The examples in that diff should give you an idea of how it works.

I think we can do a bit better than this syntax-wise, but will probably keep this approach as well for a quick and dirty way of setting out/ref args.

Interested in any feedback y'all have.

Regards,
David
Reply all
Reply to author
Forward
0 new messages