Unmock partial mocking

2 posts, 0 answers
  1. Michal
    Michal avatar
    1 posts
    Member since:
    Sep 2018

    Posted 27 Sep 2018 Link to this post

    I do partial mocking of specific method through Mock.Arrange. Is there way to unmock this method to call the original method next time ?
  2. Mihail
    Admin
    Mihail avatar
    260 posts

    Posted 28 Sep 2018 Link to this post

    Hello Michal,

    You could achieve the desired effect with sequential mocking. I would encourage you to check our help article on the subject. Here is a simple example of such a case.
    This is the class that will be tested:
    public class Bar
    {
        public virtual int GetValue()
        {
            return 99;
        }
    }
    And this is the test:
    [Test]
    public void TestMethodSequential()
    {
        // Arrange
        var bar = new Bar();
     
        Mock.Arrange(() => bar.GetValue()).Returns(25).InSequence();
        Mock.Arrange(() => bar.GetValue()).CallOriginal().InSequence();
     
        // Act & Assert - First
        var actual = bar.GetValue();
        Assert.AreEqual(25, actual);
     
        // Act & Assert - Second
        actual = bar.GetValue();
        Assert.AreEqual(99, actual);
    }

    Here any further calls to bar.GetValue after the first one will execute the original code and return the originally intended result.
    The benefit of this approach is if you would like to verify the number of occurrences.  Here is an example of how you could do this:

    [Test]
    public void TestMethodSequentialWithOccuranceAssert()
    {
        // Arrange
        var bar = new Bar();
     
        Mock.Arrange(() => bar.GetValue()).Returns(25).InSequence().OccursOnce();
        Mock.Arrange(() => bar.GetValue()).CallOriginal().InSequence().OccursOnce();
     
        // Act & Assert - First
        var actual = bar.GetValue();
        Assert.AreEqual(25, actual);
     
        // Act & Assert - Second
        actual = bar.GetValue();
        Assert.AreEqual(99, actual);
     
        Mock.Assert(bar);
    }
    If you are interested in finding more about asserting occurrences I would recommend you to check out our Asserting Occurrence help article.


    Another approach is to make a new arrangement to call the original code after the mock has done its job. Like this:
    [Test]
    public void TestMethodNewArrange()
    {
        // Arrange
        var bar = new Bar();
     
        Mock.Arrange(() => bar.GetValue()).Returns(25);
     
        // Act & Assert - First
        var actual = bar.GetValue();
        Assert.AreEqual(25, actual);
     
        // New Arrange
        Mock.Arrange(() => bar.GetValue()).CallOriginal();

        // Act & Assert - Second
        actual = bar.GetValue();
        Assert.AreEqual(99, actual);
    }


    There is a third more drastic approach to achieve your scenario by resetting all created mocks for the specific test by calling Mock.Reset() method. To further clarify this, Mock.Reset method will clear all arrangements made in the test where it is called. Here is an example of how this could be done:

    [Test]
    public void TestMethodWithMockReset()
    {
        // Arrange
        var bar = new Bar();
     
        Mock.Arrange(() => bar.GetValue()).Returns(25);
     
        // Act & Assert - First
        var actual = bar.GetValue();
        Assert.AreEqual(25, actual);
     
        Mock.Reset();
        // Act & Assert - Second
        actual = bar.GetValue();
        Assert.AreEqual(99, actual);
    }

    I would recommend you to use the first approach as it is cleaner because the arrangement is done at once place and not scattered across the whole test and it has better asserting abilities.

    I hope this information answers your questions.

    Regards,
    Mihail
    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
Back to Top