Mocking out and ref parameters

4 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
    872 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. DevCraft R3 2016 release webinar banner
  4. 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?
  5. 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.

     
Back to Top