This is a migrated thread and some comments may be shown as answers.

Usage of Returns method with out params

5 Answers 74 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Asim
Top achievements
Rank 1
Asim asked on 20 Jul 2011, 10:03 PM
Hi!

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

Sort by
0
Ricky
Telerik team
answered on 22 Jul 2011, 03:20 PM
Hi Asim,

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!

0
Asim
Top achievements
Rank 1
answered on 22 Jul 2011, 04:18 PM
Thank you Mehfuz for the response. Another, related question: Is there any way--short of writing one Arrange for each input value--to achieve the same result as what the following line of code tries to do (but does not compile; the error message I get is: "A ref or out argument must be an assignable variable")? I realize that there is nothing stopping one from writing multiple arranges with different out parameter values, but it is much more powerful to be able to arrange categories of inputs (via matchers) rather than having one or a few input from each scenario (that one is trying to cover) hard-coded.

 

Mock.Arrange(() => help.MockMe(Arg.AnyInt, out Arg.Matches<MyTypedDataset>(ds => ds.TypedDataTable[0].SomeField >= "some_value")))

 

 

 

 

 

 

0
Ricky
Telerik team
answered on 25 Jul 2011, 03:25 PM
Hi Asim,

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.

Kind Reagrds
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!

0
Asim
Top achievements
Rank 1
answered on 25 Jul 2011, 09:05 PM

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
0
Ricky
Telerik team
answered on 28 Jul 2011, 01:26 PM
Hi 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!

Tags
General Discussions
Asked by
Asim
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Asim
Top achievements
Rank 1
Share this question
or