Mock out parameter output for void method

1,263 views
Skip to first unread message

Yulia

unread,
Feb 2, 2012, 9:10:45 AM2/2/12
to NSubstitute
Hi,
How can I set up a void function to return a specific value through an
out parameter?

interface FooInterface
{
void Foo(out string arg); // naturally there's more than
one in real case
}

[TestMethod]
public void TestOutArgs()
{
FooInterface mock = Substitute.For<FooInterface>();
// to set up Foo to return "ABC" - how?
string arg;
mock.Foo(out arg);
Assert.AreEqual("ABC", arg);
}

I looked at the support for out parameters inroduced in 1.2.0.0, but
it uses .Returns() sintax, which can't be applied to a void method...
Would greatly appreciate any advice!

David Tchepak

unread,
Feb 2, 2012, 9:36:34 AM2/2/12
to nsubs...@googlegroups.com
Hi Yulia,

It is a bit messy, but you can use the When..Do syntax for this:

    [TestMethod]
    public void TestOutArgs()
    {
        FooInterface mock = Substitute.For<FooInterface>();
        string arg;
        mock.When(x => x.Foo(out arg)).Do(x => x[0] = "ABC");
        mock.Foo(out arg);
        Assert.AreEqual("ABC", arg);
    }

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.


Yulia

unread,
Feb 2, 2012, 10:07:29 AM2/2/12
to NSubstitute
Thanks for the quick reply - workd wery well!
> >http://groups.google.com/group/nsubstitute?hl=en.- Hide quoted text -
>
> - Show quoted text -

Yulia

unread,
Feb 5, 2012, 9:21:22 AM2/5/12
to NSubstitute
Another one related: when using "Returns" syntax, one can specify a
sequence of return values to return in each subsequent call.
Is there something similar for When-Do? Like, a sequence of actions to
perform each next time the When condition holds?
I implemented the behavior I needed by using acounter, but was just
wondering whether there's a build in method to do it.
> > >http://groups.google.com/group/nsubstitute?hl=en.-Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -

David Tchepak

unread,
Feb 5, 2012, 6:50:47 PM2/5/12
to nsubs...@googlegroups.com
When..Do doesn't have support for this. You could try an approach like Phil Haack used for Moq returns:


I'd recommend trying to avoid pushing this much logic into a substitute. If you need to have a lot of behaviour in a dependency to a class under test, consider refactoring that out, using a hand-coded test double or possibly even the real class.

Yulia

unread,
Feb 6, 2012, 3:53:34 AM2/6/12
to NSubstitute
Thanks David!
As I said, I managed rather easily with a counter...
The amount of logic is really the same that of multiple Returns, since
I'm using When-Do to set the return value of a function.
It's an external API that I'm mocking and all I need to simulate is
different replies it returns to me while I'm polling for a certain
one.
Thanks anyway!
> > > > >http://groups.google.com/group/nsubstitute?hl=en.-Hidequoted text -
>
> > > > - Show quoted text -- Hide quoted text -
>
> > > - Show quoted text -
>
> > --
> > 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.- Hide quoted text -

David Tchepak

unread,
Feb 6, 2012, 6:50:24 AM2/6/12
to nsubs...@googlegroups.com
Gotcha. If you'd like something reusable you could try something like this: https://gist.github.com/1751666

At the risk of being overly preachy (especially considering I've no knowledge of your exact case ;)), I try and avoid mocking external APIs too:
<gratuitousBlogPlug> http://davesquared.net/2011/04/dont-mock-types-you-dont-own.html </gratuitousBlogPlug>

I realise that in some cases it's easier and safe to mock the external API though, so glad the counter works for you, and you're welcome to try the sample in the gist if that makes it easier. :)

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