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

Using DoInstead for a private method

3 Answers 190 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Wietze
Top achievements
Rank 1
Wietze asked on 12 Dec 2012, 09:17 PM
Hi I have this situation.

Public Class PrivateMethodDoInstead
    Private Function GetSomeQuantity(ByRef quantity As Long) As Boolean
        '....get quantity from the database code would be here
        Return quantity > 0
    End Function
 
    Public Function HandleRecords() As Boolean
        Dim quantity As Long
        Dim returnValue As Boolean = GetSomeQuantity(quantity)
 
        If returnValue Then
            StoreTotals(quantity)
        End If
 
        Return returnValue
    End Function
 
    Private Sub StoreTotals(quantity As Long)
        ' do something
    End Sub
End Class

I would like to Mock my private function GetSomeQuantity.
It is private. When it is public it would work by doing this:

Mock.Arrange(Function() testInstance.GetSomeQuantity(
     Arg.IsAny(Of Long))
    ).DoInstead(Function(ByRef quantity As Long) As Boolean
                    quantity = 5
                    Return True
                End Function).Returns(True)

But how can I do this for a private function?
I did try it using the nonpublic, but it keeps giving me an error that it cannot resolve the method and that I should check my arguments.

Any help would be appreciated.

Thanks,

Wietze

3 Answers, 1 is accepted

Sort by
0
Accepted
Kaloyan
Telerik team
answered on 13 Dec 2012, 08:20 AM
Hi Wietze,

Thank you again for contacting Telerik support.

Unfortunately, we don't support mocking of out/ref arguments for non-public method. However, we are working on this and planning to include it in one of our recent builds.

As we want to deliver the best support to our users, I'd like to ask you one off-topic question that will help us do so. Your feedback will be much appreciated.
As I can see, you are using VB.Net, so I would like to ask if you are interested in having test examples in VB.Net into the JustMock documentation?

Thank you for the feedback.
 
All the best,
Kaloyan
the Telerik team
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Andrew
Top achievements
Rank 1
answered on 21 Dec 2016, 08:17 PM

I realize this post is quite old now, but it is still top Google result.  Was this ever addressed, or is mocking out/ref arguments for non-public method still not available?

Thanks,

Andrew

0
Garo
Telerik team
answered on 26 Dec 2016, 03:27 PM
Hi Andrew,

I am glad to inform you that this is now possible with JustMock. For example, here is a sample C# class that we want to test:
public class PrivateMethodDoInstead
{
    public long refArgProperty { get; set; }
  
    private bool GetSomeQuantity(ref long quantity)
    {
        //....get quantity from the database code would be here
        return quantity > 0;
    }
  
    public bool HandleRecords()
    {
        long quantity = 0;
        bool returnValue = GetSomeQuantity(ref quantity);
  
        this.refArgProperty = quantity;
  
        return returnValue;
    }
}

Using a delegate with the same signature as the private GetSomeQuantity function, you can directly write the following test:
public delegate bool MyDelegate(ref long quantity);
  
[TestMethod]
public void TestMethod()
{
    // ARRANGE
    var testInstance = new PrivateMethodDoInstead();
  
    Mock.NonPublic.Arrange<bool>(testInstance, "GetSomeQuantity", ArgExpr.Ref<long>(Arg.AnyLong))
        .Returns(new MyDelegate(delegate(ref long qty)
         {
             qty = 5;
             return true;
         }));
  
    // ACT
    var actual = testInstance.HandleRecords();
  
    // ASSERT
    Assert.AreEqual(true, actual);
    Assert.AreEqual(5, testInstance.refArgProperty);
}

The above test will assign a return value to the private function and it will arrange its ref arguments. More information regarding the Ref can be found here.

Regards,
Garo
Telerik by Progress
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
Tags
General Discussions
Asked by
Wietze
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Andrew
Top achievements
Rank 1
Garo
Telerik team
Share this question
or