Git Product home page Git Product logo

Comments (1)

dtchepak avatar dtchepak commented on June 20, 2024 1

Hi @Epicycle23 ,

Thanks for raising this. This behaviour is due to re-entrant substitute use, which is an unfortunate limitation of the trick NSubstitute's syntax uses (see also: nsubstitute/NSubstitute.Analyzers#12).

When running the Received call, NSubsitute sees something like this:

candidate.Perform(candidate.Name, new[] { "a" });
candidate
    .Received() // puts sub in "assert next call was received" mode
    .Perform(    // Perform is not run yet, as args need to be evaluated...
        candidate.Name // <-- this is the next call, and it WAS received in the line above (second line of the test)
        ...
   )  // assert passes as candidate.Name was received

We can see this by commenting out the second line:

    var candidate = Substitute.For<ICandidate>();
    // candidate.Perform("", new[] { "a" });
    candidate.Received().Perform(candidate.Name, Arg.Is<IEnumerable<string>>(new[] { "b" }));
/*
ReceivedCallsException : Expected to receive a call matching:
        Name
Actually received no matching calls.
*/

To fix this it is best to pass the values directly to the substitute:

    var candidate = Substitute.For<ICandidate>();
    candidate.Perform("test", new[] { "a" });
    candidate.Received().Perform("test", Arg.Is<IEnumerable<string>>(new[] { "b" }));
/*
.ReceivedCallsException : Expected to receive a call matching:
        Perform("test", String[])
Actually received no matching calls.
Received 1 non-matching call (non-matching arguments indicated with '*' characters):
        Perform("test", *String[]*)
*/

@tpodolak is it possible for Analyzers to detect this case with Received similar to how it works for Returns? Not sure if this is one of those cases you tried but it ended up taking too long for the analysis to run or one that could not be detected reliably. 🤔

from nsubstitute.

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.