Suppose I want to mock a method with the following signature:
public
bool MyHelperMethod(int arg1, out DataSet ds1, out DataSet ds2, out DataSet ds3, out DataSet ds4)
Is there a way to use the "Returns" method to return a value that depends on the argument values passed in, rather than returning a hardcoded value?
I am thinking of something along the lines of:
Mock.Arrange(() => helper.MyHelperMethod(Arg.AnyInt, out set1, out set2, out set3, out set4))
.DoInstead(() =>
{
// mock implementation omitted
})
.Returns(
(int arg1, out DataSet ds1, out DataSet ds2, out DataSet ds3, out DataSet ds4)
=> /* some return value computed from the arguments */);
If this is not possible (due to the "out" parameters in the method signature), can I do what I want to do by writing multiple Arrange statements, each matching on different parameter values, and then return a different value in each of these Arrange statements?
Thanks,
Asim
5 Answers, 1 is accepted
Thanks again for making the post. However, you can’t pass out arguments to Returns delegate. Here you can use the out argument as an expected parameter like the following example:
var foo = Mock.Create<IFoo>();
int
expected = 20;
Mock.Arrange(() => foo.Echo(10,
out
expected)).Returns(() => expected);
int
actual = 0;
Assert.AreEqual(expected, foo.Echo(10,
out
actual));
Assert.AreEqual(10, actual);
But since the method contains out argument, we can still pass the input arguments to Returns delegate; which is an issue to look around and therefore logging into our system as a feature request.
Optionally, you can create a support ticket (since we cant send internal builds to forum) so that i can send it to you as soon as it is ready.
Kind regards,
Mehfuz
the Telerik team
Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Mock.Arrange(() => help.MockMe(Arg.AnyInt, out Arg.Matches<MyTypedDataset>(ds => ds.TypedDataTable[0].SomeField >= "some_value")))
Thanks again for the reply.
Generally, out / ref is the expectation which will be returned when the method is executed. Currently matcher for out / ref arg is not supported. However i am logging this into our system.
Also, since you won't be able to pass Arg.Matchers for out argument directly because it(out arg) requires a variable reference. In that regard the resultant feature will look like this:
Mock.Arrange(x => x.Submit(
out
Arg.Matches<
int
>(arg => arg == 10).Value));
// or
Mock.Arrange(x => x.Submit(
out
Arg.Matches<
int
>(arg => arg == 10).Default));
//or
Mock.Arrange(x => x.Submit(
out
Arg.Matches<
int
>(arg => arg == 10).Dummy));
Let me know which one you prefer the most. I have also created an PITS entry which you can take a look here:
http://www.telerik.com/support/pits.aspx#/public/justmock/7100
Finally, if you are setting up your out /ref expectation using Arg.Matchers this won’t be though that much useful if you are not setting it in conjunction with CallOriginal. This is because you are using matcher as you don’t know what the method will return (this can only happen if you are executing the original implementation) or less you can just set the test data to a variable and then pass it as out argument.
Mehfuz
Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

I like the .Value property name the best among the three options you have listed.
As for using matchers without using CallOriginal, the benefit I see is that even when I use DoInstead, I can write one delegate that can handle all the various scenarios I might want to mock, as opposed to writing a separate Arrange for each out parameter value and a separate DoInstead delegate for each arrange.
Thanks,
Asim
Thanks for the reply. Yes you are right on the matter. Hopefully the feature will be implemented soon.
Kind Regards
Mehfuz
the Telerik team
Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!