NSubstitute icon indicating copy to clipboard operation
NSubstitute copied to clipboard

Clear configured call

Open velvolue opened this issue 1 year ago • 3 comments

Is your feature request related to a problem? Please describe. I configure and use the same mock across a test collection and some tests needs to mutate that mock to set a different return value. After that test is done, I want to reset the mock to the previous behavior. Clearing all configurations is not sufficient as I need to keep the previously configured return value.

Describe the solution you'd like I'd like to be able to use the returned ConfiguredCall to clear just that configuration.

Describe alternatives you've considered The only alternative I see is to replace the mock by manually implementing a fake, but that would require a lot of code to replace all the functionality (received calls etc.).

Additional context This would be a really nice improvement for many use-cases where you share a mock between tests.

Some example code to explain the use-case:

[Collection("myCollection")]
public class ArchiveGridDevelopmentPlanTests(MyTestFixture fixture)
{
    [Fact]
    public void Should_call_service()
    {
        var configuredCall = fixture.MockedService.GetValue().Returns(123);
        try
        {
            var sut = new ClassToTest(fixture.MockedService);
            sut.CallService().Should().Return(123);
        }
        finally
        {
            // It would be nice if this was possible...
            fixture.MockedService.RemoveConfiguredCall(configuredCall);
        }
    }
}

velvolue avatar Jun 21 '24 11:06 velvolue