Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear configured call #821

Open
velvolue opened this issue Jun 21, 2024 · 3 comments
Open

Clear configured call #821

velvolue opened this issue Jun 21, 2024 · 3 comments
Labels
feature-request Request for a new NSubstitute feature

Comments

@velvolue
Copy link

velvolue commented Jun 21, 2024

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);
        }
    }
}
@GeraldLx
Copy link

Perhaps the example does not show everything needed, but if you just want some pre defined substitute that can be customized in your tests, you could change MockedService to GetMockedService?

@velvolue
Copy link
Author

velvolue commented Jun 24, 2024

Yeah, the example is not 100%. MockedService is actually registered as a singleton in an IoC container and used for DI throughout an ASP.NET application so I need to mutate the same substitute.

@dtchepak
Copy link
Member

dtchepak commented Jul 7, 2024

Hi @velvolue ,

New calls to Returns will overwrite existing configurations. Can you overwrite it to call the base implementation instead? The most recently configuration will be used.

Clearing just something for that call sounds reasonable but is tricky to implement as we can match calls using dynamic conditions (Arg.Is() etc).

@304NotModified 304NotModified added the feature-request Request for a new NSubstitute feature label Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for a new NSubstitute feature
Projects
None yet
Development

No branches or pull requests

4 participants