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

Unmock partial mocking

1 Answer 196 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Michal
Top achievements
Rank 1
Michal asked on 27 Sep 2018, 09:16 AM
I do partial mocking of specific method through Mock.Arrange. Is there way to unmock this method to call the original method next time ?

1 Answer, 1 is accepted

Sort by
0
Mihail
Telerik team
answered on 28 Sep 2018, 07:19 AM
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
Tags
General Discussions
Asked by
Michal
Top achievements
Rank 1
Answers by
Mihail
Telerik team
Share this question
or