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

Mocking out and ref parameters

6 Answers 677 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ralf
Top achievements
Rank 1
Ralf asked on 14 Apr 2014, 08:06 PM
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

6 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 17 Apr 2014, 12:36 PM
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.

 
0
Ralf
Top achievements
Rank 1
answered on 18 Apr 2014, 08:47 AM
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?
0
Stefan
Telerik team
answered on 22 Apr 2014, 12:15 PM
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.

 
0
Amit
Top achievements
Rank 1
answered on 24 Oct 2018, 07:56 AM

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.

0
Ivo
Telerik team
answered on 24 Oct 2018, 11:03 AM
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
0
Amit
Top achievements
Rank 1
answered on 25 Oct 2018, 09:30 AM
Thank you.
Tags
General Discussions
Asked by
Ralf
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Ralf
Top achievements
Rank 1
Stefan
Telerik team
Amit
Top achievements
Rank 1
Ivo
Telerik team
Share this question
or