Giter Club home page Giter Club logo

Comments (3)

tpodolak avatar tpodolak commented on August 16, 2024

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.

from nsubstitute.analyzers.

GraniLuk avatar GraniLuk commented on August 16, 2024

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");
}

}

from nsubstitute.analyzers.

tpodolak avatar tpodolak commented on August 16, 2024

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

from nsubstitute.analyzers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.