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

False positive on NS5000 when received method is static. #199

Open
GraniLuk opened this issue Jan 30, 2023 · 3 comments
Open

False positive on NS5000 when received method is static. #199

GraniLuk opened this issue Jan 30, 2023 · 3 comments

Comments

@GraniLuk
Copy link

For example code:
transactionScopeFactory.Received(1).Create();
Analyzers makes a warning NS5000 here, when Create method is static.

@tpodolak
Copy link
Member

tpodolak commented Jan 30, 2023

Hi @GraniLuk
Static methods can not be substituted - see more in docs. One way or another, can you provide full example? Your snippet doesnt seem to be valid c#, as you cant call static method on instance of the class - so maybe we are facing other issue here.

@GraniLuk
Copy link
Author

GraniLuk commented Feb 7, 2023

Hi @tpodolak
It's about static extension methods, they can be checked with Received, but it throws a warning with latest update. Here is example of using IMemoryCache.
using Microsoft.Extensions.Caching.Memory;
using NSubstitute;
using NUnit.Framework;
using System.Threading.Tasks;

public class TestExample{

[Test]
public async Task WhenIbanNotFoundInCache_ReturnsFromHttpCallAndStoresInCache()
{
    var _memoryCache = Substitute.For<IMemoryCache>();
    var sut = new ExampleClassWithMemoryCache(_memoryCache);

    sut.InvokeMemoryCache();

    _memoryCache.Received(1).Set("key", "value");
}

}

public class ExampleClassWithMemoryCache
{
private readonly IMemoryCache _memoryCache;

public ExampleClassWithMemoryCache(IMemoryCache memoryCache)
{
    _memoryCache = memoryCache;
}

public void InvokeMemoryCache()
{
    _memoryCache.Set("key", "value");
}

}

@tpodolak
Copy link
Member

tpodolak commented Feb 7, 2023

Thanks for full example. Extension methods follow the same rules as static methods so they cannot be substituted. In certain cases however, extension method substitution might work - but this is considered a hack rather than documented behavior. Please take a look at #170 where this is explained in details by @dtchepak

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants