Suppose I want to mock a method with the following signature:
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))
// mock implementation omitted
(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?
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:
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.
the Telerik team
Mock.Arrange(() => help.MockMe(Arg.AnyInt, out Arg.Matches<MyTypedDataset>(ds => ds.TypedDataTable.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:
Let me know which one you prefer the most. I have also created an PITS entry which you can take a look here:
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. the Telerik team
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 for the reply. Yes you are right on the matter. Hopefully the feature will be implemented soon.
the Telerik team