Usage of Returns method with out params

6 posts, 0 answers
  1. Asim
    Asim avatar
    7 posts
    Member since:
    Jul 2011

    Posted 20 Jul 2011 Link to this post

    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

  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 22 Jul 2011 Link to this post

    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!

  3. DevCraft R3 2016 release webinar banner
  4. Asim
    Asim avatar
    7 posts
    Member since:
    Jul 2011

    Posted 22 Jul 2011 Link to this post

    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")))

     

     

     

     

     

     

  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 25 Jul 2011 Link to this post

    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!

  6. Asim
    Asim avatar
    7 posts
    Member since:
    Jul 2011

    Posted 25 Jul 2011 Link to this post


    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
  7. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 28 Jul 2011 Link to this post

    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!

Back to Top
DevCraft R3 2016 release webinar banner