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
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:
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()
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