Mocking out and ref parameters

7 posts, 0 answers
  1. Ralf
    Ralf avatar
    2 posts
    Member since:
    Sep 2008

    Posted 14 Apr 2014 Link to this post

    How do I mock out and ref params? Example:

    void Parent(int x) {
      var y = f(x, out y);
      ...
    }

    private int f(int x, out int y) {
    ...
    }

    How can I mock the call to f()? Does Arg.OutRefResult<T> help? I´m unfortunately unable to figure out, how it´s supposed to work.
    And there seem to be no examples online.

    -Ralf

  2. Kaloyan
    Admin
    Kaloyan avatar
    880 posts

    Posted 17 Apr 2014 Link to this post

    Hi Ralf,

    Thank you for contacting our support services.

    You can find examples at the bottom of our article about Matchers in JustMock. Also, you can search for Arg.Ref inside our examples projects (coming with JustMock).

    To assist you further, here is a concrete example for your scenario:
    The system under test
    public class PrivateFuncWithOutParam
    {
        public int outArgProperty;
     
        private int f(int x, out int y)
        {
            throw new NotImplementedException();
        }
     
        public int HandleRecords()
        {
            var result = f(12, out outArgProperty);
            return result;
        }
    }

    The test
    [TestMethod]
    public void TestMethod2()
    {
        // ARRANGE
        var testInstance = new PrivateFuncWithOutParam();
     
        Mock.NonPublic.Arrange<int>(testInstance, "f", ArgExpr.IsAny<int>(), ArgExpr.Ref<int>(Arg.AnyInt)).Returns(5);
     
        // ACT
        var actual = testInstance.HandleRecords();
     
        // ASSERT
        Assert.AreEqual(5, actual);
        Assert.AreEqual(0, testInstance.outArgProperty);
    }

    I hope this helps.

    Regards,
    Kaloyan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. Ralf
    Ralf avatar
    2 posts
    Member since:
    Sep 2008

    Posted 18 Apr 2014 in reply to Kaloyan Link to this post

    Thx for the info.

    Unfortunately it does not help much. Because I don´t want to just check, a certain type/value was passed to a mocked function.

    What I want to do is assign a value to a ref/out parameter. Like I return a value from a mocked function.

    Is that possible?
  4. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 22 Apr 2014 Link to this post

    Hello Ralf,

    Yes, it is possible. Simply pass the value you want returned to ArgExpr.Ref().

    Building on the sample system under test from the previous reply, here's the new test code:
    [TestMethod]
    public void Testestse()
    {
        // ARRANGE
        var testInstance = new PrivateFuncWithOutParam();
     
        Mock.NonPublic.Arrange<int>(testInstance, "f", ArgExpr.IsAny<int>(), ArgExpr.Ref<int>(123)).Returns(5);
     
        // ACT
        var actual = testInstance.HandleRecords();
     
        // ASSERT
        Assert.AreEqual(5, actual);
        Assert.AreEqual(123, testInstance.outArgProperty);
    }
    ArgExpr.Ref<int>(123) specifies that the value 123 will be assigned to the out parameter when the method is called.

    I hope this helps.

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  5. Amit
    Amit avatar
    2 posts
    Member since:
    Dec 2017

    Posted 24 Oct 2018 in reply to Stefan Link to this post

    Hi Stefan,

    I am facing the same issue. 

    Mock.NonPublic.Arrange<int>(testInstance, "f", ArgExpr.IsAny<int>(), ArgExpr.Ref<int>(123)).Returns(5);

     

    this doesn't work the way you described. It is taking 123 as input value instead of assigning it to the ref parameter.

  6. Ivo
    Admin
    Ivo avatar
    17 posts

    Posted 24 Oct 2018 Link to this post

    Hello Amit,

    Indeed ArgExpr.Ref method does not assign a value to the parameter. Actually, it just matches a given value. In order to achieve the desired assignment, you have to use ArgExpr.Out instead. The code will look like as following:

    Mock.NonPublic.Arrange<int>(testInstance, "f", ArgExpr.IsAny<int>(), ArgExpr.Out<int>(123)).Returns(5)

    Our documentation requires improvements regarding ArgExpr usage, but I hope that you can find some info in the following topic in JustMock API reference.

    Regards,
    Ivo
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  7. Amit
    Amit avatar
    2 posts
    Member since:
    Dec 2017

    Posted 25 Oct 2018 in reply to Ivo Link to this post

    Thank you.
Back to Top