Argument Matchers and ref/out keywords

50 views
Skip to first unread message

Bruno Silva

unread,
Oct 8, 2018, 10:35:18 AM10/8/18
to NSubstitute
Hi!
I wanna create a mock for my MySubClass. But, it has an argument ref in one of its methods.
The argument ref is an object of type MyReference. The object is created in another method, so I don't have control over the variable. 

So, there is no way to do something like this, the mock doesn't work:

var sub = Substitute.For<MySubClass>();
MyReference reference;
sub.MyMethod(Arg.Any<int>(), ref reference).Returns(x => { x[0] = new MyReference(); return true; });
CallerClass caller =new  CallerClass();
caller.CallMySubClass();

I need something this:

var sub = Substitute.For<MySubClass>();
MyReference reference;
sub.MyMethod(Arg.Any<int>(), ref Arg.Any<MyReference>()).Returns(x => { x[0] = new MyReference(); return true; });


Is there any way to use argument matchers (or another way) to solve it?

David Tchepak

unread,
Oct 8, 2018, 4:53:16 PM10/8/18
to nsubs...@googlegroups.com
Hi Bruno,

For the case you have shown I recommend using `.ReturnsForAnyArgs(x => ...)`.

In positive news, the next release of NSubstitute is getting the syntax you've shown in your second code snippet! :) (so that snippet will work unchanged!) This feature is currently in the repository at https://github.com/nsubstitute/NSubstitute so if you want to try a local build of NSubstitute you can start using this right away.

There are some more examples of working with out/ref arguments here: https://github.com/nsubstitute/NSubstitute/issues/459

Please let me know if you need more information.

Regards,
David

--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute...@googlegroups.com.
To post to this group, send email to nsubs...@googlegroups.com.
Visit this group at https://groups.google.com/group/nsubstitute.
For more options, visit https://groups.google.com/d/optout.

Bruno Silva

unread,
Oct 15, 2018, 1:27:09 PM10/15/18
to NSubstitute
Hi David!
Thank you for your reply.
It helps me :)
I will try a local build with the code, but I'm concerned if it will be compatible with my .Net version 4.0

Best Regards,
Bruno.

David Tchepak

unread,
Oct 15, 2018, 8:10:38 PM10/15/18
to nsubs...@googlegroups.com
Hi Bruno,

Unfortunately the latest builds will not work with NET40. :(

Even without the new feature you can still get out/ref to match using `ReturnsForAnyArgs`, or using matchers as per the examples in  https://github.com/nsubstitute/NSubstitute/issues/459.

Regards,
David

Message has been deleted

Bruno Silva

unread,
Dec 17, 2018, 6:49:28 AM12/17/18
to NSubstitute
Hi, 
thank you for your fast answer. 
We had a lot of a lot of work to overcome some problems, especially because your version is old (2.0.3.0, with .net4.0) :(
I hope we can update .net framework soon.
Thank you again

Regards,
Bruno.
Reply all
Reply to author
Forward
0 new messages