Comments (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)
- `Arg.Any<T>()` behaves strangely when it's stored in a variable instead of being passed directly
- Properties from a substitute object are empty when accessing in a Received.InOrder bloc
- Create a spy of Func<> and make it return a specific value for testing HOT 3
- Compatibility issue with XUnit's `IClassFixture` HOT 2
- Cannot mock an interface method that returns a generic collection with dynamic type HOT 2
- Missing/Old explanation in documentation for exception extensions HOT 3
- Customize SubstituteFactory. HOT 3
- NSubstitute seems not to recognize the parameter order correctly (string default method parameter null) HOT 1
- Mocked Method returning null? HOT 1
- Compiler error when using Arg.Is<Arg.AnyType> HOT 4
- NSubstitute keeps reference to GC collectable objects while detached from events HOT 1
- 'DidNotReceive' is recognized as 'Receive' in 'InOrder' HOT 1
- NSubstitute and Castle.Core dependency issue for net8.0 HOT 2
- How to name a satellite nuget package designed to work with NSubstitute? HOT 7
- Argument matcher for a struct with a field with non-default value HOT 3
- How to argument match the generic parameter of a Func<T> with generic constraints?
- Arg.Is<object[]> is not working as expected HOT 2
- NSubstitute makes it impossible to unit test memory leaks HOT 4
- NSubstitute Received() verifies different function in substite. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nsubstitute.