Using DoInstead for a private method

4 posts, 1 answers
  1. Wietze
    Wietze avatar
    1 posts
    Member since:
    Jul 2012

    Posted 12 Dec 2012 Link to this post

    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
  2. Answer
    Kaloyan
    Admin
    Kaloyan avatar
    874 posts

    Posted 13 Dec 2012 Link to this post

    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.
  3. Andrew
    Andrew avatar
    8 posts
    Member since:
    Jun 2014

    Posted 21 Dec 2016 in reply to Kaloyan Link to this post

    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

  4. Garo
    Admin
    Garo avatar
    109 posts

    Posted 26 Dec 2016 Link to this post

    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 matcher 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
Back to Top